If Outer is my friend, is Outer :: Inner also?

The following code compiles to MSVC:

#include <iostream>

class Bob
{        
    int a;
    friend class Outer;
};
class Outer
{      
    class Inner
    {
        void f(Bob obj)
        {
            std::cout << obj.a; //OK
        }
    };
};

      

So it seems that if Outer is Bob's friend, then Inner is automatically. I am reading the Friends chapter of the standard and cannot find a proposal that confirms or refutes this.

Is this legal, and if so, what are the chapter and verse?

+3


source to share


1 answer


[class.access.nest] / 1 indicates that

The nested class is a member and as such has the same access rights as any other member

So, I believe this is the standard behavior.



Let's say it Outer

has a member function foo()

. This function will of course have member access Bob

. As I understand it, the part I quoted implies that any nested class inside Outer

will have the same access rights as it does foo()

, thus having the ability to access members Bob

.

It's also worth noting that the standard contains the following example ( [class.friend] / 2 ), note the usage A::B

in Y

:

class A {
    class B { };
    friend class X;
};

struct X : A::B {
    // OK: A::B accessible to friend
    A::B mx; // OK: A::B accessible to member of friend
    class Y {
        A::B my; // OK: A::B accessible to nested member of friend
    };
};

      

+3


source







All Articles