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