友元函数不是类的成员函数,没有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;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-05
  • 2022-01-02
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
猜你喜欢
  • 2022-12-23
  • 2021-07-22
  • 2022-02-09
  • 2021-11-26
  • 2021-11-06
  • 2021-07-21
  • 2022-12-23
相关资源
相似解决方案