C++ - Friends (detailed explanation of friend functions, friend classes and friend member functions)

friend function

In the C++ programming language, a friend function is a special function that has access to private members of a class, although it is not a member function of the class. The existence of friend functions makes the design of the class more flexible and can grant external functions the ability to access private members of the class when needed. This article will introduce the friend function in C++ in detail, including its definition, usage scenarios, advantages and disadvantages, and examples.

1. Definition

A friend function is a non-member function declared in a class, but declared as a friend inside the class. This means that the function can access private members of the class, including private variables and private functions . The declaration of a friend function is usually inside the class declaration, but its implementation is outside the class.

1. Friend function: through friend + ordinary function
A friend function is a non-member function declared in a class, but the function is declared as a friend inside the class. This gives the function access to private members of the class.

grammar:

 friend void friendFunction(const MyClass& obj);

Case:

#include <iostream>
using namespace std;
class MyClass {
    
    
   private:
    int privateData;

   public:
    MyClass(int data) : privateData(data) {
    
    }

    // 声明友元函数
    friend void friendFunction(const MyClass& obj);

    // Getter函数
    int getPrivateData() const {
    
     return privateData; }
};

// 友元函数实现
void friendFunction(const MyClass& obj) {
    
    
    cout << "Friend function accessing private data: " << obj.privateData
         << endl;
}

int main() {
    
    
    MyClass myObj(42);
    friendFunction(myObj);  // 调用友元函数
    cout << "Private data accessed via getter: " << myObj.getPrivateData()
         << endl;

    return 0;
}

If you need to declare an ordinary function as a friend function of a certain class, you only need to make a friend declaration in the class, whether it is declared in public or private .

2. Friend class: Through friend+class,
a friend class is a class that is declared as a friend in another class. This means that a class declared as a friend can access the private members of another class, similar to the concept of friend functions, but with permission for the entire class. Friend classes are often used in situations where close cooperation between multiple classes is required, allowing these classes to access each other's private members.

grammar:

 friend class FriendClass;

Case:

#include <iostream>
using namespace std;
class MyClass;

class FriendClass {
    
    
   public:
    void accessPrivateData(const MyClass& obj);
};

class MyClass {
    
    
   private:
    int privateData;

   public:
    MyClass(int data) : privateData(data) {
    
    }

    // 声明友元类
    friend class FriendClass;

    int getPrivateData() const {
    
     return privateData; }
};

// 友元类成员函数实现
void FriendClass::accessPrivateData(const MyClass& obj) {
    
    
    cout << "Friend class accessing private data: " << obj.privateData << endl;
}

int main() {
    
    
    MyClass myObj(42);

    FriendClass friendObj;
    friendObj.accessPrivateData(myObj);  // 调用友元类的成员函数

    return 0;
}

3. Friend member function: Declare a function in the class separately as a friend function.
A friend member function is a non-member function declared in one class but declared as a friend of another class. This allows friend member functions to access private members of the authorized class. Unlike global friend functions, friend member functions can access private members of multiple classes.

grammar:

 friend int FriendMemberFunction(const MyClass& obj);

Case:

#include <iostream>
using namespace std;

class MyClass;

// 声明友元成员函数
int FriendMemberFunction(const MyClass& obj);

class MyClass {
    
    
   private:
    int privateData;

   public:
    MyClass(int data) : privateData(data) {
    
    }

    // 声明友元成员函数
    friend int FriendMemberFunction(const MyClass& obj);

    int getPrivateData() const {
    
     return privateData; }
};

// 友元成员函数实现
int FriendMemberFunction(const MyClass& obj) {
    
    
    return obj.privateData;
}

int main() {
    
    
    MyClass myObj(42);

    int data = FriendMemberFunction(myObj);  // 调用友元成员函数
    cout << "Private data accessed via friend member function: " << data
         << endl;

    return 0;
}

2. Usage scenarios

1. Access private members:
The main function is to allow external functions or classes to access the private members of another class, thereby achieving fine-grained control of the class.
2. Operator overloading:
When it is necessary to overload class operators (such as <<, >>, +, -, etc.), friend functions can access private members and implement appropriate operations.
3. Improve efficiency:
In some cases, using friend functions can improve the execution efficiency of the program because it can directly access private members of the class without going through accessor functions (getters and setters).

3. Disadvantages

1. Destruction of encapsulation:
Friend functions can break through the encapsulation of a class, allowing private members of the class to be directly accessed by external functions, which may reduce the security and maintainability of the code. (Friends also break the hiding and encapsulation of classes, so they must be used with caution (sacrifice safety, improve efficiency))
2. Difficult to maintain:
When the program becomes complex, the use of friend functions may make the code difficult to understand. And maintenance.

Friends cannot be inherited, exchanged, or passed on

Guess you like

Origin blog.csdn.net/qq_57737603/article/details/132372044