Why sub class manage to access c++ private inheritance member?
This question already has an answer here:
When you private inherit from a base class, its public members become private members of the derived class. These members are public and accessible inside of member functions of the derived class (eg B.getmultiply()
), but are private and not accessible to outside code (eg main()
) that is not a friend of the derived class.
When a class privately inherits from another, it still has access to that class's (non-private) members just like under public inheritance. It is only the outside world that doesn't have this access to them because they become private in the context of the derived class (in fact the outside world doesn't even know the derived is a derived: you can't refer to an instance of B
with a pointer of type A
for example).
上一篇: 在派生类声明中使用“Public”?
下一篇: 为什么子类管理访问c ++私有继承成员?