tags:

views:

104

answers:

2

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.

+1  A: 

The posted code looks correct though declaration of class B is missing. Post more complete snippet - you problem is likely in misplacing include files and class declarations.

Nikolai N Fetissov
Thanks. This is the root cause of my problem. I resolve that now.
michael
A: 

Yes, passing a derived-class instance to a function that takes a base-class reference is OK.

Your example compiles for me, though, and doesn't look like it should produce that error -- an "inaccessible base class" generally refers to something that was extended via protected or private inheritance rather than public inheritance. Could you post a more complete example?

Wyzard