左移运算符重载
不要随意乱用符号重载
内置数据类型 的运算符不可以重载
cout << 直接对Person自定义数据类型 进行输出
写到全局函数中 ostream& operator<< ( ostream & cout, Person & p1 ) {}
如果重载时候想访问 p1的私有成员,那么全局函数要做Person的友元函数
main.cpp
// 52.左移运算符重载.cpp : 定义控制台应用程序的入口点。
//
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
friend ostream& operator<<(ostream &cout, Person & p1);
public:
Person(){}
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
/*void operator<<() 重载左移运算符不可以写到成员函数中
{
}*/
private:
int m_A;
int m_B;
};
ostream& operator<<(ostream &cout, Person & p1) //第一个参数 cout 第二个参数 p1
{
cout << "m_A = " << p1.m_A << " m_B = " << p1.m_B;
return cout;
}
void test01()
{
Person p1(10, 10);
cout << p1 << "helloworld" << endl;//m_A = 10 m_B = 10helloworld
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}