C++ 受保护的继承是否允许派生类访问其基类的私有成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2156913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does protected inheritance allow the derived class access the private members of its base class?
提问by skydoor
I am really confused about private inheritance and protected inheritance.
我真的对私有继承和受保护的继承感到困惑。
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?
1)在protected继承中,public和protected成员成为派生类中的protected成员。在私有继承中,一切都是私有的。但是,派生类永远不能访问基类的私有成员,对吗?在这两种情况下,派生类都可以访问公共成员和受保护成员。那正确吗?
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?
2)我注意到派生类永远不会触及基类的私有成员。那么为什么私有成员会被继承呢?
回答by e.James
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.
您在第 1 点上是正确的。指定private
,protected
或public
从基类继承时不会更改派生类本身的任何访问方式。当派生类的实例在别处使用时,或者派生类恰好用作其他类的基类时,这些访问说明符告诉编译器如何处理基类成员。
UPDATE:The following may help to illustrate the differences:
更新:以下内容可能有助于说明差异:
class Base
{
private: int base_pri;
protected: int base_pro;
public: int base_pub;
};
For classes derived from base:
对于从基派生的类:
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
}
For classes derived from the 3 derived classes:
对于从 3 个派生类派生的类:
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
}
回答by EToreo
1a) Protected inheritance means the "child" can access everything it could in public inheritance, but others using that object can only see the public interface to the child, anything in it's parent is hidden.
1a) 受保护的继承意味着“子”可以访问它在公共继承中所能访问的所有内容,但其他使用该对象的人只能看到子的公共接口,其父中的任何内容都是隐藏的。
1b) Private inheritance results in all public functions of a class being inherited as private functions - meaning they can not be called from the child or accessed from a client of your object.
1b) 私有继承导致类的所有公共函数都被继承为私有函数——这意味着它们不能从子对象调用或从对象的客户端访问。
2) Private members are inherited because methods in the base class might need them to operate on.
2) 继承私有成员,因为基类中的方法可能需要它们进行操作。
回答by Mike Sherov
Yes, this is correct. Derived classes can access protected and public members of it's base class, and the derived class can not access the private members of it's base class.
The private members are inherited for the following reason: The base class can define a protected or public function that modifies the base class's private member. The derived class can call this function, and therefore needs to know about the private variable that it's accessing.
是的,这是正确的。派生类可以访问其基类的受保护成员和公共成员,而派生类不能访问其基类的私有成员。
继承私有成员的原因如下: 基类可以定义修改基类私有成员的受保护或公共函数。派生类可以调用这个函数,因此需要知道它正在访问的私有变量。
回答by dirkgently
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?
1)在protected继承中,public和protected成员成为派生类中的protected成员。在私有继承中,一切都是私有的。但是,派生类永远不能访问基类的私有成员,对吗?
Yes.
是的。
The derived class can access the public and protected members in both cases. Is that right?
在这两种情况下,派生类都可以访问公共成员和受保护成员。那正确吗?
Yes.
是的。
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?
2)我注意到派生类永远不会触及基类的私有成员。那么为什么私有成员会被继承呢?
Because they are part of the base class, and you need the base class which is a part of your derived class. Note, you can still manipulate some state (if any) maintained in the base class using non-overriden public
/protected
member functions.
因为它们是基类的一部分,您需要基类,它是派生类的一部分。请注意,您仍然可以使用非覆盖public
/protected
成员函数来操作基类中维护的某些状态(如果有)。
回答by Tyler McHenry
1) You are correct. No type of inheritance allows access to private members (only friend
declarations allow that)
1)你是对的。没有继承类型允许访问私有成员(只有friend
声明允许)
2) They're "inherited" in the sense that an object of type Derived, when stored in memory, includes all of the data members of Derived and Base, including private members of Base. The private members can't just go away since when methods of Base run on that object, they will need to access Base's private members.
2) 它们是“继承的”,因为类型 Derived 的对象在存储在内存中时,包括 Derived 和 Base 的所有数据成员,包括 Base 的私有成员。私有成员不能就这样消失,因为当 Base 的方法在该对象上运行时,他们将需要访问 Base 的私有成员。
Also, the names of the private members of Base are technically in scope in Derived's methods, but of course you'll get a compile error if you try to access them.
此外,从技术上讲,Base 的私有成员的名称在 Derived 方法的范围内,但是如果您尝试访问它们,当然会出现编译错误。