zoukankan      html  css  js  c++  java
  • 使用友元函数重载<<

    友元函数不是类的成员函数,没有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;
    }
    
  • 相关阅读:
    CGCDSSQ
    100200H
    斗地主
    借教室
    bzoj 3743
    17B
    能量项链
    589
    16-求连续数组和最大
    15-幸运数组4、7
  • 原文地址:https://www.cnblogs.com/helloweworld/p/2836541.html
Copyright © 2011-2022 走看看