Can I pass a child class to a reference of a parent class?

Posted by michael on Stack Overflow See other posts from Stack Overflow or by michael
Published on 2010-03-30T02:12:28Z Indexed on 2010/03/30 2:13 UTC
Read the original article Hit count: 271

Filed under:

Hi,

I have a class A which takes a reference of B in its constructor.

 class A {
   public: 
     A(B& b); 
  }

And I have a class SubB which is a child class of B:

class SubB : public B {
    //omitted...
 }

In my code, I create a SubB and pass it to A:

   SubB subB; 
   A a(subB);

But I get this compile Error:

error: ‘B’ is an inaccessible base of ‘SubB’

Can I pass a reference of SubB as B?

Thank you.

© Stack Overflow or respective owner

Related posts about c++