友元函数不是类的成员函数,没有this指针,所以必须在参数表中显式列出每一个操作数。
#include <iostream>
using namespace std;
class Test {
public:
int a;
int b;
Test(): a(2), b(3) {};
friend ostream& operator<< (ostream& out, const Test& t)
{
out << t.a << "hello" << t.b;
return out;
}
};
int main(void)
{
Test t;
cout << t;
return 0;
}