#include // uncomment the next line to make mutual friend classes. #define DOUBLE_FRIENDS 1 // or just delete all the pre-processor directives below. using namespace std; class OpenT { public: friend ostream & operator << (ostream & s ,const OpenT & data); friend class FillerT; #ifdef DOUBLE_FRIENDS void MakeBuddy(class FillerT & pal); #endif private: short intData; float floatData; }; class FillerT { public: #ifdef DOUBLE_FRIENDS friend class OpenT; #endif ~FillerT() { cout << "I depart with " << friendCount << " friends." << endl; } void FillFriend(OpenT & buddy) { buddy.intData = myInt; buddy.floatData = myFloat; myFloat += 2.1; ++ myInt; } private: int myInt{0}; float myFloat{1.0}; int friendCount{0}; }; #ifdef DOUBLE_FRIENDS void OpenT::MakeBuddy(class FillerT & pal) { ++pal.friendCount; } #endif ostream & operator << (ostream & s ,const OpenT & data) { s << data.intData << " " << data.floatData; return s; } int main() { OpenT someVars[10]; FillerT filler; size_t i; for(i = 0; i < 10; ++i) { filler.FillFriend(someVars[i]); #ifdef DOUBLE_FRIENDS someVars[i].MakeBuddy(filler); #endif } for(i = 0; i < 10; ++i) { cout << "someVars[" << i << "] = " << someVars[i] << endl; } return 0; }