TITLE: "Friends? Just say 'no!'" PROBLEM: Pete Becker (pete@borland.com), 1 Nov 91 [...] A very common situation that requires the use of friends creating a class that in some way imitates arithmetic. For example, here's a very incomplete complex number class: class complex { double real; double imag; public: complex( double r, double i = 0.0 ) : real(r), imag(i) {} friend complex operator + ( const complex& c1, const complex& c2 ) { return complex( c1.real + c2.real, c1.imag + c2.imag ); } }; With this class you can write code like this: complex c1( 1.0, 2.0 ); complex c2 = 1.0 + c1; RESPONSE: Ross Huitt (bytor@ctt.bellcore.com), 1 Nov 91 A 'better' solution (where 'better' == 'no friends') is: class complex { // as above without the friend complex add(const complex &c2) const; }; inline complex complex::add(const complex &c2) const { return complex(this->real + c2.real, this->imag + c2.imag); } inline complex &operator+(const complex& c1, const complex& c2) { return c1.add(c2); } This way of doing things has many advantages. For instance, if I have a large class hierarchy where all classes respond to 'ostream &<<' I only have to provide one operator function on the base class and have it call a virtual member function such as 'ostream &printOn(ostream &)' instead of writing an operator (friend or not) for each class in the hierarchy. I never use friend functions and rarely use friend classes. The way I design my systems doesn't require them.