When you use : private instead of : public. E.g.,
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
private inheritance is a syntactic variant of composition (AKA aggregation and/or has-a).
E.g., the "Car has-a Engine" relationship can be expressed using simple composition:
The "Car has-a Engine" relationship can also be expressed using private inheritance:
There are several similarities between these two variants:
There are also several distinctions:
Note that private inheritance is usually used to gain access into the protected members of the base class, but this is usually a short-term solution (translation: a band-aid).
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
Use composition when you can, private inheritance when you have to.
Normally you don't want to have access to the internals of too many other classes, and private inheritance gives you some of this extra power (and responsibility). But private inheritance isn't evil; it's just more expensive to maintain, since it increases the probability that someone will change something that will break your code.
A legitimate, long-term use for private inheritance is when you want to build a class Fred that uses code in a class Wilma, and the code from class Wilma needs to invoke member functions from your new class, Fred. In this case, Fred calls non-virtuals in Wilma, and Wilma calls (usually pure virtuals) in itself, which are overridden by Fred. This would be much harder to do with composition.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
Generally, No.
From a member function or friend of a privately derived class, the relationship to the base class is known, and the upward conversion from PrivatelyDer* to Base* (or PrivatelyDer& to Base&) is safe; no cast is needed or recommended.
However users of PrivatelyDer should avoid this unsafe conversion, since it is based on a private decision of PrivatelyDer, and is subject to change without notice.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
Similarities: both allow overriding virtual functions in the private/protected base class, neither claims the derived is a kind-of its base.
Dissimilarities: protected inheritance allows derived classes of derived classes to know about the inheritance relationship. Thus your grand kids are effectively exposed to your implementation details. This has both benefits (it allows derived classes of the protected derived class to exploit the relationship to the protected base class) and costs (the protected derived class can't change the relationship without potentially breaking further derived classes).
Protected inheritance uses the : protected syntax:
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
Take these classes as examples:
None of the derived classes can access anything that is private in B. In D_priv, the public and protected parts of B are private. In D_prot, the public and protected parts of B are protected. In D_publ, the public parts of B are public and the protected parts of B are protected (D_publ is-a-kind-of-a B). class UserClass can access only the public parts of B, which "seals off" UserClass from B.
To make a public member of B so it is public in D_priv or D_prot, state the name of the member with a B:: prefix. E.g., to make member B::f(int,float) public in D_prot, you would say:
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
E-mail the author
[ C++ FAQ Lite
| Table of contents
| Subject index
| About the author
| ©
| Download your own copy ]
Revised Aug 15, 2001