文章目录
一、运算符重载的基本概念
1.1 什么是运算符重载?
运算符重载允许我们为类类型对象定义新的运算符行为。通过重载,可以使类对象像内置类型一样使用运算符。
语法规则:
返回类型 operator运算符(参数列表) {
// 运算符逻辑
}
class Date {
public:
bool operator==(const Date& d) {
return _year == d._year && _month == d._month && _day == d._day;
}
};
1.2 运算符重载的限制
- 不能创建新运算符:如operator@。
- 不能改变内置类型的运算符行为:如int operator+(int x, int y)。
- 不能重载的运算符:::、.*、.、sizeof、?:等。
二、运算符重载的实现方式
2.1 成员函数重载
特点:运算符重载作为成员函数时,第一个操作数隐式传递给this指针。
适用场景:二元运算符(如+、-)和一元运算符(如++、--)。
示例:
class Date {
public:
bool operator==(const Date& d) {
return _year == d._year && _month == d._month && _day == d._day;
}
};
2.2 全局函数重载
特点:运算符重载作为全局函数时,所有操作数均需显式传递。
适用场景:流运算符(如<<、>>)。
示例:
ostream& operator<<(ostream& out, const Date& d) {
out << d._year << "-" << d._month << "-" << d._day;
return out;
}
三、赋值运算符重载
3.1 赋值运算符重载的作用
赋值运算符重载用于两个已存在对象间的赋值操作,确保资源正确拷贝和释放。
语法规则:
ClassName& operator=(const ClassName& other) {
// 赋值逻辑
return *this;
}
3.2 赋值运算符重载的特点
- 必须为成员函数:确保第一个操作数为当前对象。
- 返回当前对象引用:支持连续赋值(如a = b = c)。
- 默认生成:若未显式定义,编译器会自动生成默认赋值运算符重载。
class Stack {
private:
int* _a;
size_t _capacity;
public:
Stack& operator=(const Stack& other) {
if (this != &other) { // 避免自赋值
free(_a); // 释放原有资源
_a = (int*)malloc(other._capacity * sizeof(int));
memcpy(_a, other._a, other._capacity * sizeof(int)); // 深拷贝
_capacity = other._capacity;
}
return *this;
}
};
四、深拷贝与浅拷贝
4.1 浅拷贝的问题
默认赋值运算符重载对指针成员进行浅拷贝,即仅复制指针地址,而非指向的资源。这会导致以下问题:
双重释放:多个对象指向同一块内存,析构时重复释放。
悬空指针:一个对象修改或释放资源后,其他对象访问无效内存。
4.2 深拷贝的实现
深拷贝通过为新对象分配独立的内存空间,并复制原对象的内容,避免资源冲突。
示例:
class Stack {
private:
int* _a;
size_t _capacity;
public:
Stack& operator=(const Stack& other) {
if (this != &other) { // 避免自赋值
free(_a); // 释放原有资源
_a = (int*)malloc(other._capacity * sizeof(int));
memcpy(_a, other._a, other._capacity * sizeof(int)); // 深拷贝
_capacity = other._capacity;
}
return *this;
}
};
五、常见误区与陷阱
5.1 自赋值问题
在赋值运算符重载中,必须检查自赋值情况,否则可能导致资源释放错误。
cpp
复制
if (this != &other) { // 检查自赋值
// 赋值逻辑
}
5.2 连续赋值支持
赋值运算符重载应返回当前对象引用,以支持连续赋值操作。
cpp
复制
a = b = c; // 连续赋值
六、总结与最佳实践
6.1 核心要点
运算符重载:增强类类型对象的操作灵活性。
赋值运算符重载:确保对象间赋值操作的安全性。
6.2 最佳实践
深拷贝:涉及动态资源时,必须显式实现深拷贝。
自赋值检查:避免资源释放错误。
返回引用:支持连续赋值操作。