Article directory
1. Introduction to friend functions
In the C++ language, "friend functions" are functions associated with a class.
"Friend function" is not a member function of the class, but can access the private members and protected members of the class;
Friend functions can be
- global function
- Member functions of this class
- Member functions of other classes
Friend functions are friends of a class,
In the friend function, you can modify the private properties and protected properties in the class object;
Friend functions destroy the encapsulation of the class;
2. Friend function declaration
1. Friend function syntax
"Friend function" needs to be declared in the class. Use the friend keyword to modify the function. The syntax is as follows:
class ClassName {
// 成员变量和成员函数
friend returnType functionName(arguments);
};
- returnType: is the return value of the friend function;
- functionName: is the function name of the friend function;
- arguments: is the parameter list of the friend function;
2. Friend function declarations are not subject to access control restrictions
The declaration of "friend function" is not subject to class access control restrictions and can be defined anywhere, such as: declaring a friend function under private: , protected: , public: , does not affect the use of the friend function;
As long as a "friend function" is declared, no matter where it is declared, members in the class can be accessed normally through the friend function;
3. Friend function parameter requirements
In the "friend function", one parameter needs to be a pointer to the class object;
The following friend function of the Student class needs to have a parameter that is the Student class object pointer, which can access the object;
private:
// 声明友元函数
friend void changeAge(Student* s, int age);
4. Example of friend function
Declare the following friend functions in the class:
private:
// 声明友元函数
friend void changeAge(Student* s, int age);
Even if a private qualified friend function is used, the function can still be accessed outside the class;
Define the above friend function outside the class to access private members in the class object;
// 在友元函数中 访问 age 私有属性
void changeAge(Student* s, int age)
{
s->age = age;
}
3. Complete code example-friend function
In the code example below,
The Student class has a private member variable age,
And declared a friend function changeAge;
private:
// 声明友元函数
friend void changeAge(Student* s, int age);
The changeAge function is not a member function of Student, but a global function that accesses the age private member in the object through a friend relationship;
In the main function, we created a Student object and called the changeAge friend function to modify the value of the private member age;
Code example:
#include "iostream"
using namespace std;
class Student
{
public:
// 带参构造函数
Student(int age = 1, int height = 1)
{
this->age = age;
this->height = height;
cout << "执行 Student 的构造函数" << endl;
}
~Student()
{
cout << "执行 Student 的析构函数" << endl;
}
public:
// 打印类数据
void print()
{
cout << " age = " << age << " , height = " << height << endl;
}
private:
// 声明友元函数
friend void changeAge(Student* s, int age);
private:
int age; // 年龄
int height; // 身高
};
// 在友元函数中 访问 age 私有属性
void changeAge(Student* s, int age)
{
s->age = age;
}
int main() {
// 调用有参构造函数 创建 Student 实例对象
Student s(18, 180);
s.print();
changeAge(&s, 88);
s.print();
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
}
Results of the :
执行 Student 的构造函数
age = 18 , height = 180
age = 88 , height = 180
请按任意键继续. . .