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 o开发者_StackOverflow社区f ‘SubB’
Can I pass a reference of SubB as B?
Thank you.
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.
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?
精彩评论