钻石继承与混合继承修饰符(受保护的/私有的/公共的) [英] Diamond inheritance with mixed inheritance modifers (protected / private / public)

查看:79
本文介绍了钻石继承与混合继承修饰符(受保护的/私有的/公共的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有class A,B,C,D,其中A是基数,B,C在之间,而D是在钻石模型中得出的.

let's say we have class A,B,C,D where A is base, B,C are between and D is derived in diamond model.

注意:

class B私有模式继承虚拟class A

class C受保护的模式下继承虚拟class A.

class A
{
public:
    int member;  // note this member
};
class B :
    virtual private A // note private 
{

};
class C :
    virtual protected A // note protected
{

};
class D :
    public B, // doesn't metter public or whatever here
    public C
{

};

int main()
{
    D test;
    test.member = 0; // WHAT IS member? protected or private member?
    cin.ignore();
    return 0;
}

现在当我们创建class D的实例时,成员将是什么?私人还是受保护的大声笑?

now when we make an instance of class D what will member be then? private or protected lol?

图2:

如果可以的话,怎么办:

what if we make it so:

class B :
    virtual public A // note public this time!
{

};
class C :
    virtual protected A // same as before
{

};

我想member在第二个示例中会公开吗?

I suppose member will be public in this second example isn it?

推荐答案

§11.6 Multiple access [class.paths]

如果可以通过多重继承图的多个路径访问一个名称,则访问权限是拥有最多访问权限的路径. [示例:

class W { public: void f(); };
class A : private virtual W { };
class B : public virtual W { };
class C : public A, public B {
   void f() { W::f(); } // OK
};

由于W::f()在通过B的公共路径上可用于C::f(),因此允许访问. -最终示例]

Since W::f() is available to C::f() along the public path through B, access is allowed. —end example ]

我认为我不需要添加任何其他内容,但也请参见此缺陷报告(被关闭为非缺陷").

I think I don't need to add anything else, but see also this defect report (which was closed as "not a defect").

这篇关于钻石继承与混合继承修饰符(受保护的/私有的/公共的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆