Reputation: 1
Why we always say that friend function are not member function even though they are declared in the class? I have found in many books and on internet but i am not getting proper explanation.
Upvotes: 0
Views: 164
Reputation: 59912
Conceptually, a member function is of type Ret(Class::*)(Args...)
, is called on an instance of the class: instance.member_function()
, and has access to the called instance via this
. A friend function doesn't fit with "member functions". The function is defined in a separate scope and not in the class even though it looks similar to a member function declaration.
Upvotes: 0
Reputation: 33931
In [class.mfct] the C++ standard says (or said around the time of C++11. The link's bit out of date at this point)
Functions declared in the definition of a class, excluding those declared with a
friend
specifier ([class.friend]), are called member functions of that class.
I'm having difficulty finding similar wording in later drafts of the standard.
That said, I consider this one self-evident.
A friend of a class is a function or class that is given permission to use the private and protected member names from the class. A class specifies its friends, if any, by way of friend declarations. Such declarations give special access rights to the friends, but they do not make the nominated friends members of the befriending class.
A friend
is something outside the class that has been granted access by the class to the protected and private members of the class. This implies that a friend
is not a member itself.
Note also that the friend
function does not have to be implemented within the class. A declaration is sufficient.
Upvotes: 0
Reputation: 1209
If you declare a friend function that was not previously declared, that function is exported to the enclosing nonclass scope.
Upvotes: 1