Private inheritance you do not know
There are many inheritance methods in the C ++ class, and the most common method we use in software design and code writing is public inheritance, which is rarely used in private inheritance. However, we have really thought about when to use public inheritance, when to use public inheritance, and when to think about things that are often forgotten by us, let us regain its light from their forgotten corners. For example, private inheritance.
As we all know and public inheritance form an is-a relationship. What is the relationship between is-? Negative tive C ++: If you make class D inherit class B in the public form, then each object of Type D is also an object of type B, and vice versa. B is more general than D, and D is more special than B. You advocate that "B objects can be used in the same way as D Objects", because every D object is a (a) B object. If you need a D object, B object cannot be used.
In addition, we like to use virtual functions, that is, running dynamics are also built on public inheritance.
When I first learned C ++, I only knew private inheritance, so that all the inherited members (public members and protected members) became private, as to whether it has any deeper meaning and function, I will not think about it, but ideally I think that it does not want these Members to be inherited by its future subclass. What does private inheritance mean?
Let's take a look at the following code:
Class Person {...};
Class Student: private Person {...};
Void eat (const Person & p );
Person p;
Student s;
Eat (p); // correct
Eat (s); // Error
Let's look at Objective C ++'s statement:
If the inheritance relationship between classes is private, the compiler will not automatically convert a deirved class object to a base class object.
Private inheritance means implemented-in-terms-of (implemented based on something ). If you want class D to inherit class B in private mode, your intention is to use some of the features that have been prepared in class B, not because objects B and D have any conceptual relationships. Private inheritance is purely an implementation technology, meaning that only the implementation part is inherited, and the interface part should be omitted. If D inherits B in the form of private, it means that D objects are implemented based on B objects. private inheritance has no significance at the software design level, and its significance is only at the software implementation level.
Private is a bit like has-. It has the same meaning as composite. Then our problem arises again. When should we use combination and private inheritance? The answer is simple. Try to use compound. Except for the following two cases:
1. private inheritance is generally lower than compound inheritance. However, when the derived class needs to access the members of the protected base class, or the inherited virtual function needs to be redefined, private inheritance is required.
2. private inheritance can optimize empty class.