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

780 views Asked by At

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

There are 1 answers

7
Xeo On BEST ANSWER

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