c++中关系运算符重载的实现示例
在 C++ 中,关系运算符重载 允许我们为自定义类型(如类、结构体)定义关系运算(如 、!=、<、>、<=、>=)的行为,使自定义类型能像内置类型(int、float 等)一样使用关系运算符。
一、关系运算符的基本规则
1、可重载的关系运算符:、!=、<、>、<=、>=。
2、重载方式:
成员函数(推荐,尤其是</>,符合封装性);
全局函数(需访问私有成员时,可声明为友元)。
3、返回值:通常返回 bool 类型(表示运算结果的真假)。
4、对称性:!= 可基于 == 实现,>= 可基于 < 实现,避免重复代码。
5、const 修饰:若重载为成员函数,建议加 const(保证对常量对象也能调用)。
二、核心示例:自定义类的关系运算符重载
以 Person 类为例,重载 ==、!=、<(按年龄比较):
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name;
int age;
public:
// 构造函数
Person(string n, int a) : name(n), age(a) {}
// ========== 1. 重载 == (成员函数 + const) ==========
bool operator==(const Person& other) const {
// 自定义比较规则:姓名和年龄都相同则相等
return (this->name == other.name) && (this->age == other.age);
}
// ========== 2. 重载 != (基于 == 实现) ==========
bool operator!=(const Person& other) const {
return !(*this == other); // 复用 ==,避免重复逻辑
}
// ========== 3. 重载 < (按年龄比较) ==========
bool operator<(const Person& other) const {
return this->age < other.age;
}
// ========== 4. 重载 > (基于 < 实现) ==========
bool operator>(const Person& other) const {
return other < *this; // 复用 <,简化逻辑
}
// 辅助函数:打印信息
void show() const {
cout << "姓名:" << name << ",年龄:" << age << endl;
}
};
int main() {
Person p1("张三", 20);
Person p2("李四", 25);
Person p3("张三", 20);
// 测试 ==
if (p1 == p3) {
cout << "p1 和 p3 相等" << endl;
} else {
cout << "p1 和 p3 不相等" << endl;
}
// 测试 !=
if (p1 != p2) {
cout << "p1 和 p2 不相等" << endl;
}
// 测试 <
if (p1 < p2) {
cout << "p1 年龄小于 p2" << endl;
}
// 测试 >
if (p2 > p1) {
cout << "p2 年龄大于 p1" << endl;
}
return 0;
}
三、全局函数重载(友元版)
若需通过全局函数重载(例如需要对称处理左操作数为内置类型的情况),可将函数声明为类的友元:
class Person {
private:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
// 声明友元函数(重载 ==)
friend bool operator==(const Person& p1, const Person& p2);
// 声明友元函数(重载 !=)
friend bool operator!=(const Person& p1, const Person& p2);
};
// 实现友元函数(==)
bool operator==(const Person& p1, const Person& p2) {
return p1.name == p2.name && p1.age == p2.age;
}
// 实现友元函数(!=)
bool operator!=(const Person& p1, const Person& p2) {
return !(p1 == p2);
}
四、关键注意事项
1、避免冗余:
!= 基于 == 实现,>= 基于 < 实现,<= 基于 > 实现,减少代码重复。
示例:bool operator>=(const Person& other) const { return !(*this < other); }
2、const 正确性:
成员函数重载时,必须加 const(保证常量对象可调用)。
函数参数也建议加 const(避免修改实参)。
3、对称性:
若重载为全局函数,左 / 右操作数类型对称(如 operator==(int, Person) 和 operator==(Person, int))。
成员函数重载时,左操作数固定为类对象(如 p1 < p2 等价于 p1.operator<(p2))。
4、不要改变运算符语义:
重载的关系运算符应符合直觉(如 == 表示 “相等”,而非其他逻辑)。
5、编译器默认行为:
C++ 不会为自定义类默认生成 ==、< 等关系运算符(需手动重载)。
但会默认生成拷贝赋值运算符(=),需注意区分。
五、常见场景
自定义数据结构(如链表、树)的元素比较;
排序算法(如 std::sort)中,自定义类对象的排序规则(需重载 <);
容器查找(如 std::find)中,判断元素是否相等(需重载 ==)。
例如,使用 std::sort 排序 Person 数组(需重载 <):
#include <algorithm>
#include <vector>
int main() {
vector<Person> vec = {Person("张三", 25), Person("李四", 20), Person("王五", 30)};
sort(vec.begin(), vec.end()); // 依赖 operator< 重载(按年龄排序)
for (const auto& p : vec) {
p.show(); // 输出:李四(20)、张三(25)、王五(30)
}
return 0;
}
通过合理重载关系运算符,可让自定义类型的使用更接近内置类型,提升代码可读性和易用性。
到此这篇关于c++中关系运算符重载的实现示例的文章就介绍到这了,更多相关c++ 关系运算符重载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!


最新评论