Some summary of friend functions in C++

  • 2020-04-02 01:38:14
  • OfStack

1. A brief introduction of friend functions

1.1 why use friend functions

In the implementation of data sharing between classes, reduce system overhead, improve efficiency. If A function in class A accesses A member of class B (for example, an implementation of the smart pointer class), the function in class A is A friend of class B. Specifically, in order for member functions of other classes to directly access private variables of that class. That is, it allows external classes or functions to access private and protected variables of a class, thus allowing both classes to share the same function.

In fact, there are about two situations where you need to use friend functions :(1) some situations where operator overloading requires the use of friend functions. (2) when two classes want to share data.

1.2 advantages and disadvantages of using friend functions

1.2.1 advantages: can improve efficiency, simple and clear expression.

1.2.2 disadvantages: friend functions break the encapsulation mechanism, try not to use member functions, only use friend functions if necessary.

2. Use of friend functions

2.1 parameters of friend function:

Since the friend function does not have this pointer, there are three conditions for the argument:

2.1.1 to access a non-static member, an object is required as a parameter;

2.1.2 to access static members or global variables, the object is not required as a parameter;

2.1.3 if the object that takes the parameter is a global object, the object is not required to take the parameter;

2.2 position of friend function

Because a friend function is an out-of-class function, its declaration can be placed in a private or public segment of the class without distinction.

2.3 calls to friend functions

You can call friend functions directly without passing objects or Pointers

2.4 classification of friend functions:

Depending on the source of this function, it can be divided into three methods:

2.4.1 ordinary function friend function

2.4.1.1 purpose: to enable ordinary functions to access the friends of a class

2.4.1.2 grammar:

Declaration: friend + normal function declaration

Implementation location: can be outside or in a class

Implementation code: same as normal functions

Call: similar to a normal function, called directly

2.4.1.3 code:


class INTEGER
 {
  friend void Print(const INTEGER& obj);//Declare friend functions
 };
void Print(const INTEGER& obj ) 
{
   //The body of the function
}
void main()
{
  INTEGER obj;
  Print(obj);//Direct call
}

2.4.2 all member functions of class Y are friend functions of class x-friend classes

2.4.2.1 purpose: use a single declaration to make all functions of class Y friends of class X, which provides a way for objects of class Y to cooperate between classes so that they can have functions of class X and class Y.

2.4.2.2 grammar:

Declare location: public or private, often written as private (think of a class as a variable)

Declaration: friend + class name (not object)

2.4.2.3 code:


class girl;
class boy
{
public:
  void disp(girl &);
};
void boy::disp(girl &x) //The function disp() is a member of the boy class and a friend of the girl class
{
  cout<<"girl's name is:"<<x.name<<",age:"<<x.age<<endl;//With the help of friends, in the member function disp of boy, directly access the private variable of girl with the help of the object of girl
}
class girl
{
private : 
  char *name;
  int age;
  friend boy; //Declare that a boy is a friend of a girl
};

The main function is not going to be the same as it would be in a normal call.

2.4.3 a member function of class Y is a friend function of class X

2.4.3.1 purpose: make a member function of class Y a friend of class X. Specifically, in this member function of class Y, with the help of the parameter X, the private variable of X can be directly used

2.4.3.2 grammar:

Declare location: declare in public (itself a function)

Declaration: declaration of the friend + member function

Call: first define Y object Y -- call its member function with Y -- its member function USES the friend mechanism

2.4.3.3 code:

The implementation code is similar to the implementation in 2.4.2.3 except that it changes to friend void boy::disp(girl &); Take care of it yourself...

Summary: some overloaded implementations of operators are also implemented outside of the class, so it is usually necessary to declare friends of the class.

4. Difference between friend function and member function of class

4.1 member functions have this pointer, while friend functions do not.

4.2 friend functions cannot be inherited, just as a father's friend is not necessarily a son's friend.


Related articles: