nested-classes/nested-2-f14.cpp - Class Outer that contains class Inner - friends of each other. | Lecture 5 - slide 12 : 40 Program 2 |
// The starting point reproduced from page 851-852 of "The C++ Programming Language", 3ed version. // With friendship between Outer and Inner classes. // Solves visibility problems from earlier version of the program. Compiles. #include <iostream> #include <string> class Outer{ private: typedef int T; int i; public: int i2; static int s; class Inner{ friend class Outer; // Outer is a friend of Inner private: int x; T y; public: void fi(Outer *p, int v); }; int fo(Inner* p); }; void Outer::Inner::fi(Outer *op, int v){ op->i = v; // i is still visible via op op->i2 = v; } int Outer::fo(Inner* ip){ ip->fi(this,2); return ip->x; // Now x in Inner is visible } // - because Outer is a friend of Inner int main(){ Outer o; Outer::Inner i; i.fi(&o, 5); }