Friends of C++ : details of friend functions and friend classes

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

I. introduction of you yuan
We know that a member function of a class can access other member functions of the same class, including public, private, and protected members. The external functions of the class can only access the public members of the class.

A friend is a mechanism that allows nonclass member functions to access nonpublic members of a class.
You can specify a function as a friend of a class or an entire class as a friend of another class.

A friend function
Friends metaclass

Two, friend function
The friend function is defined outside the class scope, but it needs to be explained in the class body
To distinguish it from the member functions of the class, the function is defined by using the keyword friend in the class, in the following format:

friend   Type friend function name (parameter table);
The function of friend is to improve the running efficiency of the program

Notes for friend function:
1.
Friend function is not a member of the class function, in the function body to access the members of the object, you must use the object name plus the operator ". "plus the object member name. But friend functions can access all members of a class (public, private, protected), and functions can only access public members of a class.

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

3, The scope of a friend function of a class is not that class scope. If the friend function is a member function of another class, its scope is of another class, otherwise it is the same as a normal function.

4, Friend functions break the encapsulation of object-oriented programming classes, so use them as little as possible if they are not required. Or other means to ensure encapsulation.


#include <math.h>
#include <iostream>
using namespace std;
class Point
{
    friend double Distance(const Point &p1, const Point &p2);
public:
    Point(int x, int y);
private:
    int x_;
    int y_;
};
Point::Point(int x, int y) : x_(x), y_(y)
{
}
double Distance(const Point &p1, const Point &p2)
{
    double dx = p1.x_ - p2.x_;
    double dy = p1.y_ - p2.y_;
    return sqrt(dx * dx + dy * dy);
}
int main(void)
{
    Point p1(3, 4);
    Point p2(6, 9);
    cout << Distance(p1, p2) << endl;
    return 0;
}

Distance is a friend function of the Point class, and private data members of the class can be accessed.

Friend class
If the member function of some class B will frequently access the data member of another class A, and the Private/Protectd restriction of the data member of A causes trouble for the access of B, B can only access indirectly through the member function of A's Public
Make B the friend of class A, that is, class A opens its Private/Protectd content to class B, let B access directly
A friend: A class can be a friend of another class
All member functions of a friend class are friend functions of another class
Declaration of friend metaclass:
Friend class class name;

Notes for friends:
1. Friendship is one-way
2. Friend relationships cannot be passed
3. Friend relationships cannot be inherited

TeleController. H:


#ifndef  _TELE_CONTROLLER_H_
#define _TELE_CONTROLLER_H_
class Television;
class TeleController
{
public:
    void VolumeUp(Television &tv);
    void VolumeDown(Television &tv);
    void ChanelUp(Television &tv);
    void ChanelDown(Television &tv);
};
#endif // _TELE_CONTROLLER_H_ 

TeleController. CPP:

#include "TeleController.h"
#include "Television.h"
void TeleController::VolumeUp(Television &tv)
{
    tv.volume_ += 1;
}
void TeleController::VolumeDown(Television &tv)
{
    tv.volume_ -= 1;
}
void TeleController::ChanelUp(Television &tv)
{
    tv.chanel_ += 1;
}
void TeleController::ChanelDown(Television &tv)
{
    tv.volume_ -= 1;
} 

Television. H:

#ifndef _TELEVISION_H_
#define _TELEVISION_H_
class TeleController;
class Television
{
    friend class TeleController;
public:
    Television(int volume, int chanel);
private:
    int volume_;
    int chanel_;
};
#endif // _TELEVISION_H_

Television. CPP:

#include "Television.h"
Television::Television(int volume, int chanel) : volume_(volume), chanel_(chanel)
{
} 

The main. CPP:

#include "Television.h"
#include "TeleController.h"
#include <iostream>
using namespace std;
int main(void)
{
    Television tv(1, 1);
    TeleController tc;
    tc.VolumeUp(tv);
    return 0;
} 

Treat the TeleController class as a friend of the Television class, so that the member functions of the TeleController class have access to all members of the Television class, including private members.


Related articles: