C++ friend function and friend class details

  • 2020-05-26 09:42:04
  • OfStack

C++ friend function and friend class details

In general, friends are divided into two types: friend functions and friend classes. A friend is a mechanism for a non-public member of a class to be accessed by a non-member function of the class. You can specify a function as a friend of a class, and this function is called a friend of that class. You can also specify class A as the friend of class B, then class A is the friend of class B, and all member functions of class A are the friend functions of class B, and all members of class B can access the non-public members of class B.

Matters needing attention of friend function:

(1) friend function is not a member function of the class, in the function body to access the members of the object, must use the "object name. Object member" to access, friend function can access all the members of the class (public, private, protected), and 1-like function can only access the public members of the class.

(2) the friend function is not restricted by the access rights keyword in the class, and it can be placed in the public, private and protected parts of the class. The result is the same.

(3) the scope of a friend function of a class is not the scope of this class. If the friend function is a member function of another class, its scope is the scope of another class. Otherwise, it is the same as a function of 1.

(4) friend functions destroy the encapsulation characteristics of object-oriented design. So use as little as possible.

Friends metaclass

If a member function of class B frequently accesses the data member of class A, and the private/protected restriction of the data member of class A causes the trouble of access of class B, B can only access indirectly through the member function of class A Public. In this case, class B can be used as the friend class of class A, that is, class A provides private and protected member access to class B, making class B directly accessible.

All member functions in a friend class can be considered as friend functions of another class.

Friend class declaration: friend chass class name;

Here is an example of a friend function

Friend functions can be declared in a class by adding the friend keyword before the function, and then defined outside the class or directly in the class. Either way is fine. The following example takes the form of declaration in a class and definition outside of a class.


class Point 
{ 
  friend double Distance(const Point &p1, const Point &p2); // Declaration in the body of the class  
 
public: 
  Point(int x, int y):x_(x), y_(y) 
  {}; 
 
private: 
  int x_; 
  int y_; 
}; 
 
// Class defines a friend function outside of the class  
double Distance(const Point &p1, const Point &p2) 
{ 
  double dx = p1.x_ - p2.x_; // Access the private member of the class directly  
  double dy = p1.y_ - p2.y_; 
  return sqrt(dx*dx+dy*dy); 
} 
 
int main() 
{ 
  Point p1(3,4); 
  Point P2(6,8); 
  cout << Distance(p1,p2) << endl; 
 
  return 0; 
} 

Here's an example of a friend class:


class Television // TV class  
{ 
  friend class TeleController;// Friend class declaration  
   
public: 
  Television(int volume, int chanel): volume_(volume), chanel_(chanel){} 
 
private: 
  int volume_; // The volume  
  int chanel_; // channel  
}; 
 
class TeleController // The remote control type  
{ 
public: // Each member function in the class is a friend function of the TV class and can access the private data member of the TV class  
  void VolumeUp(Television &tv) 
  { 
    tv.volume_ += 1; // The volume and 1 
  } 
 
  void VolumeDown(Television &tv) 
  { 
    tv.volume_ -= 1; 
  } 
 
  void ChanelUp(Television &tv) 
  { 
    tv.chanel_ += 1; // Channel add 1 
  } 
 
  void ChanelDown(Television &tv) 
  { 
    tv.chanel_ -= 1; // Channel to reduce 1 
  } 
}; 
 
 
int main() 
{ 
  Television tv(1,1); 
  TeleController tc; 
  tc.ChanelUp(tv); 
  return 0; 
} 

The friend class needs to pay attention to the following points:

(1) the friendship relationship is one-way;

(2) the friend relationship cannot be passed: A is the friend of B, B is the friend of C, A is the friend of C, and this transmission is not valid

(3) the friend relationship cannot be inherited: A is the friend of B, and C, a derived class of A, is also the friend of B, which is not true.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: