Fork me on GitHub


[C++] Nested Class

A nested class is declared within the scope of another class. The name of a nested class is local to its enclosing class. Unless you use explicit pointers, references, or object names, declarations in a nested class can only use visible constructs, including type names, static members, and enumerators from the enclosing class and global variables.

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 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


  1. The inner class idiom

– EOF –