C ++: friend

Ordinary friend function

Friend class is only an external function, and defines its outer

Tomomoto类

In a statement in class a1 friend classes (friend class a2) do not need to #include "a2.h", in the class a2 where to call members of the class a1, the need for #include "a1"

Note: You can not call each other on the header where the two classes

Like a friend of a member function (more complicated)

  • The first will be declared as a friend class class a2 be written, its internal functions (friend will be declared as a member function class) declared, does not define, attention to the need for class a1; declared may be in the internal function declarations in use
  • The class a1 to write, because to declare a friend class member function, you need to #include "a2" friend class :: func () in the class;
  • Unable to call each other, so be #include "a1.h" in class a2.cpp, the definition of each class member function a2

Click here for details

Added:
If you want to call themselves his friend function, we must remember that a friend function declaration is only a process of giving permission, not a true statement
I would like to call themselves, you must first declare the true friend

Guess you like

Origin www.cnblogs.com/Liberavi/p/11568484.html