C ++中的公共和私有继承 [英] Public and private inheritance in C++
本文介绍了C ++中的公共和私有继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从文献中我们知道对于公共继承,子类(子类)的对象也可以被认为是基类(超类)的对象。为什么当继承受保护或私有时,子类的对象不能被认为是超类的对象?
As we know from the literature for the public inheritance the object of child class (sub-class) also can be considered as the object of base class (super-class). Why the object of the sub-class can’t be considered as an object of super-class, when the inheritance is protected or private?
推荐答案
因为你看不到它:
class Base
{
public: virtual ~Base() {}
};
class PublicDerived: public Base
{ };
class PrivateDerived: private Base
{ };
int main()
{
PublicDerived publicD;
PrivateDerived privateD;
Base& base1 = publicD;
Base& base2 = privateD; // ERROR
}
$ b $ p
所以你不能使用PrivateDerived对象,
所以它永远不会像一个Base类对象。
So you can not use a PrivateDerived object where a Base object could be used.
So it will never act like a Base class object.
这篇关于C ++中的公共和私有继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文