From: http://www.cppblog.com/twzheng/articles/21020.html
Youyuan functions and youyuan classes
Data is hidden and encapsulated using the class mechanism. Data members of the class are generally defined as private members, and member functions are generally defined as common, provides communication interfaces between classes and the outside world. However, sometimes some functions need to be defined. These functions are not part of the class, but need to frequently enumerate the data members of the class. In this case, these functions can be defined as the friend functions of the function. In addition to the youyuan function, there are also youyuan classes, both collectively referred to as youyuan. Youyuan improves the program running efficiency (that is, it reduces the time overhead required for Type checks and security checks), but it destroys the encapsulation and hiding of classes, this allows non-member functions to invoke private members of the class.
Youyuan Function :
A friend function is a non-member function that can directly invoke private members of a class. It is a common function defined outside the class. It does not belong to any class, but needs to be declared in the class definition. When declaring it, you only need to add the keyword friend before the name of the youyuan, the format is as follows:
Function name of the friend type (form parameter );
The Declaration of the friend functions can be placed in the private or public part of the class. They are no different. They all indicate that they are a friend function of the class.
A function can be a membership function of multiple classes. You only need to declare it in each class.
The method and principle of calling a friend functions are the same as that of a common function.
Youyuan class :
All member functions of the youyuan class are the youyuan functions of the other class and can access the hidden information (including private and protected members) in the other class ).
When you want a class to be able to access the private members of another class, you can declare this class as another type of membership class. The statement format for defining a friend Meta class is as follows:
Friend class name;
Among them: Friend and class are keywords, and the class name must be a defined class in the program.
For example, the following statement indicates that Class B is a friend class of Class:
Class
{
...
Public:
Friend Class B;
...
};
After the preceding descriptions, all member functions of Class B are membership functions of Class A. They can access private and protected members of Class.
Note:
(1) Friendship cannot be inherited.
(2) The relationship between friends and friends is unidirectional and not interchangeable. If Class B is a friend of Class A, Class A is not necessarily a friend of class B. It depends on whether there is a corresponding declaration in the class.
(3) The relationship between friends and friends is not transmitted. If Class B is a friend of Class A, Class C is a friend of Class B, and class C is not necessarily a friend of Class A, it also depends on whether there is a corresponding statement in the class