在一些工程中我们经常需要访问某些类的静态成员,添加一个友元函数可以达到我们的目的。
#include <iostream>
#include<cstring>
using namespace std ;
class Student{
private:
string name; int age; int sore;
public:
Student(string name,int age,int sore)
{
this->name = name;
this->age = age;
this->sore = sore;
}
friend void Display(Student student);
} ;
void Display(Student student)
{
cout<<student.name<<"\t"<<student.age<<"\t"<<student.sore<<endl;
}
int main(void)
{
Student a("TOM",13,96);
Display(a);
return 0;
}