Friend.cc
#include <iostream> using namespace std; class Y; //(A) class X { int m; int n; public: X( int mm, int nn ) { m = mm; n = nn; } friend class Y; //(B) friend void print( X* ); //(C) }; class Y { X* x; int t; public: Y( X* xobj ) { x = xobj; t = x->m + x->n; } //(D) int get_t() { return t; } }; void print( X* ptr ) {cout << ptr->m << " " << ptr->n << endl;} //(E) int main() { X* ptr = new X(100, 200); Y y( ptr ); cout << y.get_t() << endl; // 300 print( ptr ); // 100 200 return 0; }
Maintained by John Loomis, last updated 30 Dec 2006