C++运算符重载
1. 概念
赋予运算符更多的功能。
2. 内容
- 赋值运算符
+ - * / %
运算符- 自增自减运算符
- 输出流运算符的重载
<<
- 输入流运算符的重载
>>
- 函数调用运算符
()
- 下标运算符
[]
- 成员访问运算符
->, *
3. 赋值运算符
这个一般是用已存在的对象赋值给另一个已存在的对象。
//如存在 Computer 类。
Computer pc1("xiaomi",1000);
Computer pc2("huawei",2000);
pc2 = pc1; // 表示将 pc1 的 值赋给 pc2。
// 如果p2不存在,则,Computer pc2 = pc1; 则会调用拷贝运算符。
四点要素:
- 自身判断
- 释放之前的内存
- 深拷贝
- 返回
*this
Computer &operator=(const Computer &rhs) {
if (this != &rhs) {
delete[] _brand;
_brand = new char[strlen(rhs._brand) + 1]();
_price = rhs._price;
}
return *this;
}
4. + - * / %
双目运算符,可使用普通函数的形式, 成员函数或友员函数。
此处使用友员函数。
friend Point operator+(const Point &lhs, const Point &rhs);
Point operator+(const Point &lhs, const Point &rhs) {
cout << "Point operator+(const Point&, const Point&)" << endl;
return Point(lhs._ix + rhs._ix, lhs._iy + rhs._iy);
}
- * / %
和+
是类似的。
5. 自增自减
前置和后置是不同的,
前置返回自身,表达式更新为++或--后的值。
后置返回右值,表达式为之前的值。
//前置:
Point &operator++() {
++_ix;
++_iy;
return *this;
}
//后置:返回不是 &,为了返回临时变量, int参数是为了区分前置和后置。
Point operator++(int) {
Point tmp(_ix, _iy);
++_ix;
++_iy;
return tmp;
}
6. 输出流运算符 <<
只能用友元函数。
因为要访问类内部成员,所以只有友元和成员。
成员则多一个 this
,不符合运算符重载的规则。
流没用拷贝构造和=,只能使用引用。
friend ostream &operator<<(ostream &os, const Point &rhs);
//const 为了能够接收右值,& 少一次拷贝构造。
ostream &operator<<(ostream &os, const Point &rhs) {
os << "(" << rhs._ix << "," << rhs._iy << ")" << endl;
return os;
}
未完待续....
代码
Point.cc
#include <iostream>
#include <ostream>
using std::cout;
using std::endl;
using std::ostream;
class Point {
public:
Point() {
}
Point(int ix, int iy)
: _ix(ix)
, _iy(iy) {
cout << "Point(int, int)" << endl;
}
Point(const Point &rhs)
: _ix(rhs._ix)
, _iy(rhs._iy) {
cout << "Point(const Point&)" << endl;
}
Point &operator=(const Point &rhs) {
cout << "Point& operator=(const Point&)" << endl;
if (this != &rhs) {
_ix = rhs._ix;
_iy = rhs._iy;
}
return *this;
}
Point &operator++() {
++_ix;
++_iy;
return *this;
}
Point operator++(int) {
Point tmp(_ix, _iy);
++_ix;
++_iy;
return tmp;
}
friend Point operator+(const Point &lhs, const Point &rhs);
friend ostream &operator<<(ostream &os, const Point &rhs);
private:
int _ix;
int _iy;
};
Point operator+(const Point &lhs, const Point &rhs) {
cout << "Point operator+(const Point&, const Point&)" << endl;
return Point(lhs._ix + rhs._ix, lhs._iy + rhs._iy);
}
ostream &operator<<(ostream &os, const Point &rhs) {
os << "(" << rhs._ix << "," << rhs._iy << ")" << endl;
return os;
}
void test0() {
}
int main(int argc, char *argv[]) {
test0();
cout << endl;
return 0;
}
Computer.cc
#include <iostream>
using std::cout;
using std::endl;
class Computer {
public:
Computer() {
}
Computer(const char *brand, double price)
: _brand(new char[strlen(brand) + 1]())
, _price(price) {
cout << "Computer(const char*, double)" << endl;
strcpy(_brand, brand);
}
Computer(const Computer &rhs)
: _brand(new char[strlen(rhs._brand) + 1]())
, _price(rhs._price) {
cout << "Computer(const Computer&)" << endl;
strcpy(_brand, rhs._brand);
}
Computer &operator=(const Computer &rhs) {
if (this != &rhs) {
delete[] _brand;
_brand = new char[strlen(rhs._brand) + 1]();
_price = rhs._price;
}
return *this;
}
private:
char *_brand;
double _price;
};
void test0() {
}
int main(int argc, char *argv[]) {
test0();
cout << endl;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具