About C ++ private inheritance and c private inheritance
Many C ++ programmers have never used private inheritance to design their classes. Indeed, if private inheritance is used but public inheritance is used, the implementation of program functions is not affected. However, this misuse is a misaligned description, which may lead to misunderstandings of readers and even the loss of class users. When writing a class declaration, we are actually designing an intent. What designers need is precise description.
- Private inherited string outsourcers
To explain private inheritance, Let's first look at co-occurrence inheritance to make a comparison.
Public inheritance is essentiallyIs-. For example, to describe a type of car, you can perform the following design:
class Car{};class Jeep : public Car{}
Private inheritance, subclass can only be in
ClassUse the features of the parent class, instead of opening the features of the parent class to the outside in the form of interfaces, that is,
Subclass objectYou cannot directly use the functional interfaces of the parent class. Therefore, the meaning of private inheritance becomes:
Sub-classes can use the features of the parent class to implement their own functions.. This is typical
Has-.
class Engine{public: void Run();};class Car : private Engine{public: void Drive() { // let engine go! Run(); }};
The link description is equivalent to a combination, for example:
class Car{ ...private: Engine m_engine;}
- Private inheritance VS combination
Private inheritance brings too much program overhead, but the combination is short and fast. Therefore, when designers want to describe the relationship between Has-,Combination is preferred.
However, you must use private inheritance in either of the following ways: