对于类内部的私有成员如果外部的函数想要直接访问类的私有成员是不合法的,而友元函数却能合法访问。
#include <iostream> #include <string> using namespace std; class Student { private: string name; int age; public: Student(string name, int age) { this->age = age; this->name = name; } friend void getName(Student* s); }; void getName(Student* s) { cout << "My friend's name is " << s->name << endl; } int main() { Student s("eta", 0); getName(&s); return 0; }
标签:友元,name,int,age,Student,include,getName From: https://www.cnblogs.com/meetalone/p/17152861.html