|
||
|
Why friend functions are needed
Passing arguments through the argument list
All friend functions pass all their arguments through the argument list, and each argument value is subject to
assignment-compatible conversions. Here is an example:
class pandaBear {
...
pandaBear (int);
int memberFunction();
friend int nonMemberFunction(const pandaBear&);
}
class designPattern {
...
designPattern (int);
int memberFunction();
friend int nonMemberFunction(const designPattern&);
}
When a member function is invoked, no user-defined conversions are applied. Calling 5.memberFunction(); will produce an error
since the conversion tweedleDee (5), which converts from int type to tweedleDee type, is not applied to make
a tweedleDee out of 5.
On the other hand, this conversion will be applied to the argument of friendFunction() in the following call: nonMemberFunction (5);
Therefore if an implicit function argument type conversion is desired in an operation, the function implementing this operation must
be a nonmember function.
|
||
|
|
||