Public inheritance means that everyone knows 17 that Derived is derived from Base.
Protected 16 inheritance means that only Derived, friends 15 of Derived, and classes derived from Derived 14 know that Derived is derived from Base.*
Private 13 inheritance means that only Derived and 12 friends of Derived know that Derived is 11 derived from Base.
Since you have used private 10 inheritance, your main() function has no 9 clue about the derivation from base, hence 8 can't assign the pointer.
Private inheritance 7 is usually used to fulfill the "is-implemented-in-terms-of" relationship. One 6 example might be that Base exposes a virtual 5 function that you need to override -- and 4 thus must be inherited from -- but you don't 3 want clients to know that you have that 2 inheritance relationship.
*also: how much 1 wood would a woodchuck chuck...
From a common understanding of inheritance, C++’ “private 15 inheritance” is a horrible misnomer: it 14 is not inheritance (as far as everything outside 13 of the class is concerned) but a complete 12 implementation detail of the class.
Seen 11 from the outside, private inheritance is 10 actually pretty much the same as composition. Only 9 on the inside of the class do you get special 8 syntax that is more reminiscent of inheritance 7 than composition.
There’s a caveat though: C++ syntactically 6 treats this as inheritance, with all the 5 benefits and problems that this entails, such 4 as scope visibility and accessibility. Furthermore, C-style casts (but no 3 C++ cast!) actually ignores visibility and 2 thus succeeds in casting your
Derived pointer to 1
Base* bPtr = (Base*) new Derived();
Needless to say, this is evil.
private means "implementation detail", which 8 makes the fact that
Derived derives from
Base an implementation 7 detail.
Private inheritance is not interface 6 inheritance, but implementation inheritance. It 5 doesn't implement an "Is-A" relationship, but 4 an "Is-Implemented-Using" relationship.
Derived isn't 3 a
Base as far as users of the classes are concerned, it 2 just happens to (currently) be implemented 1 using it.
With private inheritance, you lose the option 2 to treat your derived object as an object 1 of your base class.
If you inherit privately any code that requires 2 the conversion from Derived* to Base* must 1 be a member or a friend of the Derived class.
More Related questions