关系运算符重载
作用:重载关系运算符,可以让两个自定义类型对象进行对比操作
示例:
class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; }; bool operator==(Person & p) { if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) { return true; } else { return false; } } bool operator!=(Person & p) { if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) { return false; } else { return true; } } string m_Name; int m_Age; }; void test01() { //int a = 0; //int b = 0; Person a("孙悟空", 18); Person b("孙悟空", 18); if (a == b) { cout << "a和b相等" << endl; } else { cout << "a和b不相等" << endl; } if (a != b) { cout << "a和b不相等" << endl; } else { cout << "a和b相等" << endl; } } int main() { test01(); system("pause"); return 0; }
实例
一、定义一个日期类用于测试
class Date { public: Date(int year = 1900, int month = 1, int day = 1):_year(year),_month(month),_day(day) {} void print()//输出日期 { cout << _year << "/" << _month << "/" << _day << endl; } private: int _year; int _month; int _day; };
正常情况下如果想比较两个日期大小是无法实现的,这是因为运算符默认都是给内置类型用的。
int main() { Date d1(2022, 2, 21); Date d2(2022, 2, 23); Date d3(2022, 2, 24); //d1 == d2;直接比较会导致无法编译 return 0; }
二、重载运算符== 函数名:operator加上运算符 参数:有几个操作数就有几个参数,参数类型就是要操作对象的类型 返回值:看运算符运算后的返回值是什么
//存在this指针,要少传一个参数 bool operator==(const Date& x)//引用节省空间,const保护实参 { return _year == x._year && _month == x._month && _day == x._day; }
公有函数无法访问私有变量,所以运算符重载要写在类内当作成员函数c
三、日期赋值= 参数类型 返回值 检测是否自己给自己赋值 返回 * this 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。 如果不写,会默认生成赋值重载,和拷贝构造行为类似,内置类型会完成值拷贝,自定义类型成员会调用他的赋值重载
//赋值重载 Date operator=(const Date& d)//返回值类型是Date { _year = d._year; _month = d._month; _day = d._day; return *this;//支持连续赋值,this是当前对象的别名,拷贝构造。 }
测试结果
int main() { Date d1(2022, 2, 21); Date d2(2022, 2, 23); Date d3(2022, 2, 24); d1 == d2; //d1.operator== (d2);//可以调用但可读性差 //d1 == d2;//编译器自动转换为 d1.operator== (d2); d1 = d3; d1.print(); //赋值运算符重载:用于两个已经定义出的对象间的拷贝赋值 //拷贝构造:一个对象准备定义时,用另一个对象来初始化他 Date d4(d3); d4.print(); Date d5 = d3;//这里是拷贝构造,只要是创建时定义就是拷贝构造,注意区分赋值重载。 d1 = d3 = d2;//连续赋值,链式编程思想 d1.print(); return 0; }