5

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.

Community
  • 1
  • 1
Oliver
  • 27,510
  • 9
  • 72
  • 103

1 Answers1

4

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.

jrok
  • 54,456
  • 9
  • 109
  • 141
  • 1
    I upvoted this answer, but it is not clear to me why that quote implies that the rights are indirectly inherited. I mean, `C3` is still a nested class of a nested class. Does "nested of a nested" mean "nested"? – imreal Feb 21 '14 at 18:38
  • @Nick the rule can be applied recursively, I guess. – jrok Feb 21 '14 at 18:41
  • +1 for quote but where does it say that membership is transitive? the clause implies that C2 is a member of C1 so C2 members have access to C1 private members; and C3 is a member of C2 so it has access to C2 private members; and although it is sensible to have membership be transitive, that clause doesn't say it is, or that it can be applied recursively. – Oliver Feb 21 '14 at 19:02
  • 1
    @Schollii After some searching I'm unable to find a quote that would explicitly say membership is transitive. This is the best I've got for now, sorry :) – jrok Feb 21 '14 at 21:36
  • @jrok Effort appreciated! – Oliver Oct 15 '16 at 01:37