全局函数做友元作用:
可以访问类中 private 的变量
(可以想象中,类表示此函数很友好,允许它访问自己的私有变量)
#include<iostream>
#include<cstdio>
using namespace std;
class T{
friend void f(T tmp);
public:
int a = 10;
private:
int b = 11;
};
void f(T tmp){
cout<<tmp.a<<" "<<tmp.b<<endl;
cout<<"可以访问tmp.b了"<<endl;
}
int main()
{
T t = T();
f(t);
return 0;
}