问题
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 toC::f()
along the public path throughB
, 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