Path: utzoo!utgpu!water!watmath!clyde!att!whuts!mhuxh!mhuxu!mhuxt!mhuxi!mhuhk!mhuxo!ulysses!andante!alice!bs From: bs@alice.UUCP (Bjarne Stroustrup) Newsgroups: comp.lang.c++ Subject: Re: Friend specifier considered harmful Summary: useful for operators Message-ID: <8163@alice.UUCP> Date: 3 Sep 88 14:32:30 GMT References: <61@cybaswan.UUCP> <1988Sep2.174327.6439@ateng.uucp> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 37 A friend function can be used to express that coercions is acceptable for the left hand operand of an operator. Such coercions are not aceptable for the left hand operand of an operator defined as a member function: class Int { // ... public: Int(int); // e.g. Int(27) Int(string); // e.g. Int("123456789123456789") friend Int operator+(Int,Int); friend Int operator*(Int,Int); // ... Int operator=(Int); // ... }; main() { Int a = 1; a+1; // operator+(a,Int(1)) 1+a; // operator+(Int(1),a) a = 1; // a.operator+(Int(1)) 1 = a; // illegal } This could be expressed without the use of friend functions (but not in C++, since you cannot specify a member function for a built-in type so int::operator(Int) cannot be defined). It could, however, also be expressed without the use of member functions. It can also be expressed without the use of coercions, but that leads to an explosion of operator functions.