Does a nested class have access to its owner's private variables?

advertisements

This is blowing my mind. I will post code if I need to, only if the answer is "No, it shouldn't do that."

The question is simple:

Should a class nested inside an "owning" class (for lack of a better word) be able to access the owning class's private variables via its own pointer to an instance of the owning class?

Because that is what I am seeing; I am using 2011 version of C++. Is this expected?


Quoting from C++ standard N3485 class.access.nested:

A nested class is a member type 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

class E {
    int x;
    class B { };
    class I {
       B b; // OK: E::I can access E::B
       int y;
       void f(E* p, int i) {
          p->x = i; // OK: E::I can access E::x
       }
   };
   int g(I* p) {
      return p->y; // error: I::y is private
   }
};

You can find information about Clause 11 here: C++ Standard papers N3485(need to view the file through the link or download it)