1. Public inheritance indicates that the is-a relationship requires full inheritance of the interface, while private inheritance indicates that "the relationship based on something" requires only inheritance, and there are two rules for private inheritance:
1). All members of a base class that are inherited through private are converted to private properties in the derived class
2). Because of 1), the compiler does not allow a derived class to be converted to a base class to prevent illegal access to the private members of the derived class.
2. The terms 38,private inheritance and recombination have the same effect-"to achieve out of something". Between the two, use the compound as much as possible, unless necessary. The necessary conditions refer to when the protected member and the virtual function are involved and in some extreme cases (explained later) Under strict space constraints.
Benefits of compounding for private inheritance:
Suppose you want to implement a widget class, and it uses a time class, that is, to "implement the Widget class based on the time class,"
If you inherit with private, it might look like this:
class Widget:private timer{public: ... Private : ...}
View Code
If you use a composite, it might look like this:
class widget{public: ... Private : Time timer; ...}
View Code
(Set the timer to private because the Widget class is not intended to open the interface to the client for time)
There are two aspects that can reflect the benefits of a composite for private inheritance:
1). Private inheritance cannot prevent the widget's derived class from redefining a timer's virtual function, which is probably what the widget designer doesn't want to see.
2). Inheritance improves compilation dependencies between files because the widget inherits time, so the header file that defines the widget needs to include the header file that defines the time, and if you use a composite, you only need to replace it with time* in the widget.
Of course, there are two cases where using private inheritance is better:
1). "When one intends to become a derived class wants to access a protected part of the base class, or to redefine one or more virtual functions, the conceptual relationship between these two classes is actually ' Is-implementations-in-terms-of ' rather than is-a '.
2). Private inheritance can make the compiler ebo (empty base optimization, the white-space basis optimization), the so-called blank base class optimization, that is, for the following empty class:
class empty{}
If the following composite is used:
class holdsanint{ int A; Empty e;}
View Code
Then a Holderasint class will take up more than 4 bytes (which could take up to 8 bytes), because for a "zero-size independent (non-dependent) object", the C + + official requires a char into which to support access. (also due to byte alignment, Holdersasint may occupy 8 bytes)
With private inheritance, a holdersasint requires only 4 bytes, and the empty object will no longer occupy space, i.e.:
class Holdersasint:private empty{ int A;}
View Code
This is the compiler's implementation of the Ebo. Of course, the "empty classes" used in reality does not really contain anything, although they do not have non-static objects, but there are often typedefs,enum,static member variables, or non-virtual functions ( STL has many technical uses of the empty classes, which contain useful members (usually typedefs), including base classes Unary_function and Binary_function, which are "user-defined function objects" Classes, which is usually inherited, can avoid an increase in derived classes size due to EBO).
3. Summary:
1). Private inheritance and compounding all mean is-implementation-in-terms-of, but the composition is easier to understand, the preference is to compound.
2). Private inheritance may be the best choice in the face of two classes that do not have a is-a relationship and one that needs to access another protected member or need to redefine its virtual function, in addition, the Ebo allowed by private composite is dedicated to "minimizing object size" Library developers may be important.
Effective C + + clause 39 judicious and prudent use of private inheritance