// Inspirred from page 851-852 of "The C++ Programming Language", 3ed version. // Intended to show that instances of an inner class does not have access to private members // in instances of an outer class (as described in "The C++ Programming Language"). // Does not compile. #include #include class Outer{ private: typedef int T; // Type T and int i are private in Outer int i; public: int i2; static int s; class Inner{ private: int x; T y; // OK - Access to types in enclosing class, such as T, is allowed. public: void fi(Outer *op, int v){ op->i = v; // OK - Access to private members in enclosing class via an object (here *op). // A nested class has access to members of its enclosing class, even to private members // (via a pointer to instance of the enclosing class). op->i2 = v; // OK - i2 is public. } }; // End of Inner int fo(Inner* ip){ ip->fi(this,2); // OK - Inner::fi is public return ip->x; // error: Inner::x is private. // From Outer we cannot access private members in a nested class // (via a pointer to an instance of the nested class). } }; int main(){ Outer o; Outer::Inner i; i.fi(&o, 5); o.fo(&i); }