Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner class access to outer several levels up

The following builds in VS 2010:

class C1
{
private:
    enum E {E_VAL};
    static void methC1() {}

public:
    class C2
    {
    public:
        class C3
        {
        public:
            void methC3() 
            {
                int a=E_VAL; // this surprised me
                methC1();    // and this too
            }
        };
    };
};

int main() 
{
    C1::C2::C3 obj;
    obj.methC3();
}

Is this standard? I saw this other SO post where an answer compares inner classes to friend classes, but a friend of a friend is not a friend, so wondering what standard says.

like image 439
Oliver Avatar asked Oct 02 '22 12:10

Oliver


1 Answers

The standard says (draft n3337, 11.7 [class.access.nest]):

1 A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules (Clause 11) shall be obeyed. [...]

And that's pretty much it. C3 has got the same access right as, say, methC1.

like image 51
jrok Avatar answered Oct 05 '22 12:10

jrok