0

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.

  • 3
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Mar 22 '18 at 19:42

3 Answers3

1

friend (C++)

If you declare a friend function that was not previously declared, that function is exported to the enclosing nonclass scope.

Smit Ycyken
  • 1,189
  • 1
  • 11
  • 25
0

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.

[class.friend] states:

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.

user4581301
  • 33,082
  • 7
  • 33
  • 54
0

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.

kmdreko
  • 42,554
  • 6
  • 57
  • 106