Home > Article > Backend Development > Detailed explanation of C++ friend functions: How are friend functions used in different compilation units?
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 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
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!