Home  >  Article  >  Backend Development  >  Detailed explanation of C++ friend functions: How are friend functions used in different compilation units?

Detailed explanation of C++ friend functions: How are friend functions used in different compilation units?

WBOY
WBOYOriginal
2024-04-28 13:45:011000browse

Friend functions allow functions to access private members of other classes. When a friend function and the class it accesses are not in the same compilation unit, there are two scenarios: the friend function is declared in the header file: the declaration of the friend function is in the header file of the class, and the definition of the class is in a different source file , friend functions can access private members. Friend functions are declared in source files: The declaration and definition of friend functions are located in independent source files. Friend functions need to be declared in the header file before they can be used in other compilation units.

C++ 友元函数详解:友元函数在不同编译单元的使用?

C Detailed explanation of friend functions: The use of friend functions in different compilation units

Friend functions are a type of C A special type of function that can access private members of other classes. This allows friend functions to perform operations that other classes cannot, such as modifying private data or calling private methods.

Friend functions in different compilation units

When you organize your code into different compilation units (such as source files or header files), the friend functions Usage becomes more complex. For a friend function to be able to access the private members of another class, the class must be declared before the friend function is declared.

If the friend function and the class it accesses are not in the same compilation unit, there are two different scenarios:

Scenario 1: The friend function is declared in the header file

In this case, the declaration of the friend function is in the header file of the class, and the definition of the class is in a different source file. This is the most common case:

// header.h
class MyClass {
    // ...
    friend void myFriendFunction();
};

// source.cpp
#include "header.h"
void myFriendFunction() {
    MyClass obj;
    // 可以访问 MyClass 的 private 成员
    obj.privateMember = 10;
}

Scenario 2: Friend function declared in source file

In this less common case, friend function The declaration and definition are located in separate source files:

// class.cpp
class MyClass {
    // ...
    friend void myFriendFunction();
};

// friend.cpp
#include "class.h"
void myFriendFunction() {
    MyClass obj;
    // 可以访问 MyClass 的 private 成员
    obj.privateMember = 10;
}

For scenario 2, you also need to declare friend functions in the header file for use in other compilation units:

// header.h
class MyClass;  // 前向声明
extern void myFriendFunction();

Practical Case

Consider the following example of using a friend function to operate a private member:

#include <iostream>

class MyClass {
    int privateMember;
public:
    MyClass(int val) : privateMember(val) {}
    friend void printPrivate(MyClass& obj);
};

void printPrivate(MyClass& obj) {
    std::cout << "Private member: " << obj.privateMember << std::endl;
}

int main() {
    MyClass obj(10);
    printPrivate(obj);  // 可以访问 privateMember

    return 0;
}

In this example, printPrivate() The friend function can Accesses the private member privateMember of MyClass and prints it to the console.

Notes

  • Friend functions should be used with caution because they will break class encapsulation.
  • Use friend functions only when you really need to access private members.
  • Clearly document the purpose and usage of friend functions.

The above is the detailed content of Detailed explanation of C++ friend functions: How are friend functions used in different compilation units?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn