受保护的继承是否允许派生类访问其基类的私有成员? [英] Does protected inheritance allow the derived class access the private members of its base class?
问题描述
我真的很困惑私人继承和保护继承。
I am really confused about private inheritance and protected inheritance.
1)在受保护的继承中,public和protected成员在派生类中成为受保护的成员。在私人继承中,一切都是私有的。但是,派生类永远不能访问基类的私有成员,是不是?在这两种情况下,派生类都可以访问公共和受保护的成员。是对的吗?
1) in protected inheritance, the public and protected members become protected members in the derived class. In the private inheritance, everything is private. However, the derived class can never access the private members of the base class, is that right? The derived class can access the public and protected members in both cases. Is that right?
2)我注意到基类的私有成员不会被派生类触及。那么为什么私人成员继承了?
2) I noticed that the private members of the base class will never be touched by the derived class. So why are the private members inherited?
推荐答案
你在第1点是正确的。从基础继承时指定 private
, protected
或 public
类不会在派生类本身上改变任何访问方式。这些访问说明符告诉编译器当派生类的实例在其他地方使用时如何处理基类成员,或者派生类恰好用作其他类的基类。
You are correct on point #1. Specifying private
, protected
or public
when inheriting from a base class does not change anything access-wise on the derived class itself. Those access specifiers tell the compiler how to treat the base-class members when instances of the derived class are used elsewhere, or if the derived class happens to be used as a base class for other classes.
UPDATE: 以下可能有助于说明差异:
UPDATE: The following may help to illustrate the differences:
class Base
{
private: int base_pri;
protected: int base_pro;
public: int base_pub;
};
对于从基础派生的类:
class With_Private_Base : private Base { void memberFn(); };
class With_Protected_Base : protected Base { void memberFn(); };
class With_Public_Base : public Base { void memberFn(); };
// this would be the same for all of the above 3 classes:
void With_PXXX_Base::memberFn()
{
base_pri = 1; // error: `int Base::base_pri' is private
base_pro = 1; // OK
base_pub = 1; // OK
}
对于从3个派生类派生的类:
For classes derived from the 3 derived classes:
class A : public With_Private_Base { void memberFn(); }
void A::memberFn()
{
base_pri = 1; // error: `int Base::base_pri' is private
base_pro = 1; // error: `int Base::base_pro' is protected
base_pub = 1; // error: `int Base::base_pub' is inaccessible
}
class B : public With_Protected_Base { void memberFn(); }
void B::memberFn()
{
base_pri = 1; // error: `int Base::base_pri' is private
base_pro = 1; // OK
base_pub = 1; // OK
}
class C : public With_Public_Base { void memberFn(); }
void C::memberFn()
{
base_pri = 1; // error: `int Base::base_pri' is private
base_pro = 1; // OK
base_pub = 1; // OK
}
对前三个派生类的外部访问:
External access to the first three derived classes:
void main()
{
With_Private_Base pri_base;
pri_base.base_pri = 1; // error: `int Base::base_pri' is private
pri_base.base_pro = 1; // error: `int Base::base_pro' is protected
pri_base.base_pub = 1; // error: `int Base::base_pub' is inaccessible
With_Protected_Base pro_base;
pro_base.base_pri = 1; // error: `int Base::base_pri' is private
pro_base.base_pro = 1; // error: `int Base::base_pro' is protected
pro_base.base_pub = 1; // error: `int Base::base_pub' is inaccessible
With_Public_Base pub_base;
pub_base.base_pri = 1; // error: `int Base::base_pri' is private
pub_base.base_pro = 1; // error: `int Base::base_pro' is protected
pub_base.base_pub = 1; // OK
}
这篇关于受保护的继承是否允许派生类访问其基类的私有成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!