Diamond inheritance with mixed inheritance modifers (protected / private / public)

守給你的承諾、 提交于 2020-01-24 05:43:07

问题


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.

NOTE:

class B inherits virtualy class A in private mode,

class C inherita virtualy class A in protected mode.

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;
}

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

Figure No2:

what if we make it so:

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

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

};

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


回答1:


§11.6 Multiple access [class.paths]

If a name can be reached by several paths through a multiple inheritance graph, the access is that of the path that gives most access. [ Example:

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
};

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").



来源:https://stackoverflow.com/questions/8957187/diamond-inheritance-with-mixed-inheritance-modifers-protected-private-publi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!