在类内的公有权限区域使用friend关键字声明 友元函数
在类外进行友元函数的定义
#include <iostream>
#include <string>
using namespace std;
class Student{
public:
friend void getData(Student &student); // 在公有权限区域声明友元函数
string school = "CTGU";
Student(){
name = "yxr";
age = 18;
}
~Student(){
cout << "析构函数的调用" << endl;
}
private:
string name;
int age;
};
void getData(Student &student){
cout << student.school << endl;
cout << student.name << endl;
cout << student.age << endl;
}
int main(){
Student student;
getData(student);
system("pause");
return 0;
}