// A7.cpp - private void constructor used by a friend // private copy constructor used by another friend // // To create objects, even by a friend, we need another constructor. // Otherwise, where would we get an object to copy. - I've provided // a void constructor in the private area. #include using namespace std; class X { public: friend X* XCreator() { return new X; } friend void XCopier(const X &x) { X newX = x; newX.say(); } void say() const { cout << "\n an X object here!"; } private: X() { } X(const X &x) { } }; ///////////////////////////////////////////////////////////////// // This function won't compile since it is not a friend // // X* AnotherXCreator() { return new X; } void main() { ///////////////////////////////////////////////////////////////// //X x; this won't compile, since default constructor is private. X *pX = XCreator(); pX->say(); ///////////////////////////////////////////////////////////////// // Note that even though XCopier is a friend it can not accept // or return X objects by value. It can only make copies internally. XCopier(*pX); cout << "\n\n"; }