配合友可输出自定义数据类型;
左移运算符 << 只能通过全局函数定义;
注意返回cout才能够实现链式编程;
#include<iostream>
#include<string>
using namespace std;
class Person
{ ///配合友元能够实现自定义数据类型;
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person()
{
m_a = 10;
m_b = 10;
}
private:
int m_a;
int m_b;
};
//左移运算符 << 只能通过全局函数定义;
ostream& operator<<(ostream & cout, Person & p)
{
cout << "m_a="<<p.m_a <<" m_b=" << p.m_b;
return cout; //注意返回cout才能够实现链式编程;
}
int main()
{
Person p;
cout << p << " end" << endl;
system("pause");
return 0;
}