#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
friend ostream& operator<<(ostream &cout, Person & p1);
public:
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
//试图利用成员函数 做<<重载
//void operator<<( Person & p) // p.operator<<(cout) p<<cout//但没办法让cout在左边,不符合习惯
//{
//
//}
private:
int m_A;
int m_B;
};
//利用全局函数实现左移运算符重载
ostream& operator<<(ostream &cout/*起别名*/, Person & p1)
{
cout << "m_A = " << p1.m_A << " m_B = " << p1.m_B;
return cout;//从而实现链式编程操作
}
//改成out也可以正常运行
//原名别名还可以混着用
void test01()
{
Person p1(10, 10);
cout << p1 << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
返回引用,就是函数在做了一些运算之后,返回一个cont的东西,如果不返回的话,就没有办法调用它。