Re: friend declared function call
On Apr 11, 3:16 am, Ian Collins <ian-n...@hotmail.com> wrote:
Christopher wrote:
On Apr 10, 7:16 pm, Sven K=F6hler <please-remove-skoeh...@upb.de> wrote:=
so i have some strange class declaration, which looks like this:
namespace wurst {
class bla {
friend bool foobla(const bla& v, int x) {
return true;
}
};
}
Someone got confused there.
A friend function shouldn't be implemented in the class it is a friend
of. Otherwise, it wouldn't be a friend, but a method belonging to the
class instead.
That simply isn't true. A friend has to be declared and may
also be defined within the class definition.
It's actually a fairly standard idiom for operators in general
(although it may be applied for normal functions as well). In a
lot of cases, in fact, the friend declaration is present not
because the function requires access to private members, but
because it allows definition in the class. The standard Barton
and Nackman trick, for example. (I use it regularly for
generating the binary operators from the <op>= forms, for
example. Or for generating a complete STL iterator interface
from a small set of iterator primitives.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34