I have learned from these terms:
1.private inheritance differs from two other types of inheritance, and derived class objects cannot be implicitly converted to base class objects. The following code:
class Bird//鸟{};class ostrich:private Bird//鸵鸟{};int main(){Bird *b = new ostrich();//编译不通过,基类不能转换为派生类}
The compiler explicitly states that the base class is inaccessible, so the conversion fails. Private inheritance has no so-called inheritance between the base class and the derived class, and its existence is purely for application-level service, that is, the inheritance relationship for the flexibility of programming.
2. The following code tells us the benefits of private invocation, and when there is a special case where the base class size is empty, calling private inheritance can minimize the amount of space the derived class occupies. Then someone would say that my defined class is not empty, why size is 1 bytes, here is a simple explanation. The size of any one class is not empty, even if you define
Class m{}; The size of the classes is still 1, the size of the class is only related to variables, and variables are non-static variables, and static variables do not affect the size of the class.
#include <string>#include <iostream>using namespace STD;classBird//Bird{ Public:Static stringNameStatic stringGetName () {returnName }};classOstrich:PrivateBird//Ostrich{};intMain () {intM1 =sizeof(Bird);//m1 size is 1 bytes intM2 =sizeof(ostrich);//m2 size is also 1 bytes}
Effective C + + clause 39