#include<iostream>
using namespace std;
class Person {
public :
friend ostream & operator<<(ostream &cout, Person p);
Person(int a, int b) {
m_A = a;
m_B = b;
}
//利用成员函数重载p.operator(p)
//void operator<<(cout) 简化版 p<<cout 不可能实现
//只能用全局函数重载左移运算符
private:
int m_A;
int m_B;
};
//全局函数左移重载运算符 本质 operator<<(cout,p) 简化版cout<<p
//ostream 对象只能有一个
ostream & operator<<(ostream &cout, Person p)
{
cout << "m_A=" << p.m_A<<"m_B"<< p.m_B;
return cout;
}
void test01()
{
Person p(10,10);
cout << p<<"eeeee"<<endl;//链式编程
}
int main()
{
test01();
system("pause");
return 0;
}
总结:把友元函数和运算符重载实现自定义数据输出