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

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

NOTE:

class B inherits virtualy class A in 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 that we are creating an instance of class D , what will the member be? private or protected lol?

Figure 2:

what if we do this:

 class B : virtual public A // note public this time! { }; class C : virtual protected A // same as before { }; 

I believe member will be public in this second example, right?

+4
source share
1 answer

§11.6 Multiple access [class.paths]

If the name can be reached in several ways through a graph with multiple inheritance , this is access to the path that gives the 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 for C::f() on a public path through B , access is allowed. -end example]

I think I don’t need to add anything, but see also this defect report (which was closed as “not defective”).

+6
source

Source: https://habr.com/ru/post/1392264/


All Articles