4

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?

codekiddy
  • 5,897
  • 9
  • 50
  • 80

1 Answers1

7

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

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • @Kerrek: Thanks for the edit, I couldn't remember how to seamlessly embed code in a quote. :) – Xeo Jan 22 '12 at 01:11
  • That was I saying im my previous comment :D public has more "power" then private or protected. nice example thanks alot. but this is not the case with PRIVATE/PROTECTED combination! inheritance list has more power in that case :) cool. – codekiddy Jan 22 '12 at 01:13
  • @codekiddy: I don't quite understand. If the example was changed from `public virtual/private virtual` to `protected virtual/private virtual`, the behaviour would still be the same - the call to `W::f()` would be allowed because of the protected access path through `B` - the order in the inheritance list (base-class list) has nothing to do with anything as far as I can see. – Xeo Jan 22 '12 at 01:18
  • Oh sory, I was talking aobut calling function f() from an instance of object C (in your example) in functio main! not calling inside a class like W::F() :D call it from an instance and you'll see hehe – codekiddy Jan 22 '12 at 01:26
  • ADD: remove funciton f() {(W::f(); } and the call it from an instance of C or copy my first example! – codekiddy Jan 22 '12 at 01:27
  • @codekiddy: Well, of course. The most accessible path is `protected`, and you can't call protected stuff outside of the inheritence hierarchy. – Xeo Jan 22 '12 at 01:41
  • @Xeo, Visual studio says: "can't acces protected member" then if I swap inheritance list it say's "can't access private member". as you said that compiler does not implement those rules corectly :( – codekiddy Jan 22 '12 at 01:52