使用普通函数做为C++类的友元函数必须通过的类对象的引用来访问来的成员
#include <iostream>
#include <string>
using namespace std;
class print{
public:
print(string sstr = "NULL") :str(sstr) {
}
void exect();
friend void change(print &p);
~print(){}
private:
string str;
};
void print::exect()
{
cout << str << endl;
}
void change(print &p)
{
p.str = "i am your friends";
}
int main()
{
print p("I am yu");
change(p);
p.exect();
return 0
}
执行之后的打印结果为:“I am your friends”;