运算符重载

我们使用operator来重载运算符。

什么是运算符重载

运算符重载就是让结构体也能使用普通的运算符,比如 +, -, *, == 等等。

基本格式

1
2
3
返回值类型 operator 运算符(参数) {
// 实现代码
}

一元运算符重载

正号和负号运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Point{
int x, y;

// 重载一元+运算符: +x -> (Point)
Point operator+() const {
return Point(x, y); // 返回本身
}

// 重载一元-运算符: -x -> (Point)
Point operator-() const {
return Point(-x, -y); // 返回相反数
}
};

// 使用示例:
Point p(3, 4);
Point pos = +p; // 调用 operator+
Point neg = -p; // 调用 operator-

自增运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Counter{
int value;

Counter(int v = 0) : value(v) {}

// 前置自增: ++x -> (Counter)
Counter& operator++() {
this->value++; // this->value += 1
return *this; // 返回当前对象的引用
}

// 后置自增: x++ -> (Counter, int)
Counter operator++(int) {
Counter old = *this; // 保存原来的值
this->value++;
return old; // 返回原来的值
}
};

// 使用示例:
Counter c(10);
++c; // 调用 operator++
Counter d = c++; // 调用 operator++(int)

二元运算符重载

算术运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
struct Point{
int x, y;

Point(int x = 0, int y = 0) : x(x), y(y) {}

// 重载加法: x+y -> (Point, Point)
Point operator+(const Point& other) const {
return Point(this->x + other.x, this->y + other.y);
}

// 重载减法: x-y -> (Point, Point)
Point operator-(const Point& other) const {
return Point(this->x - other.x, this->y - other.y);
}

// 重载乘法: x*y -> (Point, int)
Point operator*(int scalar) const {
return Point(this->x * scalar, this->y * scalar);
}

// 重载除法: x/y -> (Point, int)
Point operator/(int scalar) const {
return Point(this->x / scalar, this->y / scalar);
}
};

// 使用示例:
Point a(1, 2), b(3, 4);
Point c = a + b; // 调用 operator+
Point d = a - b; // 调用 operator-
Point e = a * 5; // 调用 operator*
Point f = a / 2; // 调用 operator/

比较运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
struct Point{
int x, y;

Point(int x = 0, int y = 0) : x(x), y(y) {}

// 重载相等: x==y -> (Point, Point)
bool operator==(const Point& other) const {
return this->x == other.x && this->y == other.y;
}

// 重载不等: x!=y -> (Point, Point)
bool operator!=(const Point& other) const {
return !(*this == other); // 复用上面的==
}

// 重载小于: x<y -> (Point, Point)
bool operator<(const Point& other) const {
if(this->x != other.x) {
return this->x < other.x;
}
return this->y < other.y;
}

// 重载大于: x>y -> (Point, Point)
bool operator>(const Point& other) const {
return other < *this; // 复用上面的<
}
};

// 使用示例:
Point a(1, 2), b(1, 2), c(2, 3);
bool eq = (a == b); // true
bool neq = (a != b); // false
bool less = (a < c); // true

赋值运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
struct Point{
int x, y;

Point(int x = 0, int y = 0) : x(x), y(y) {}

// 重载赋值: x=y -> (Point, Point)
Point& operator=(const Point& other) {
if(this != &other) { // 防止自己赋值给自己
this->x = other.x;
this->y = other.y;
}
return *this; // 返回当前对象的引用
}

// 重载+=: x+=y -> (Point, Point)
Point& operator+=(const Point& other) {
this->x += other.x;
this->y += other.y;
return *this;
}

// 重载-=: x-=y -> (Point, Point)
Point& operator-=(const Point& other) {
this->x -= other.x;
this->y -= other.y;
return *this;
}
};

// 使用示例:
Point a(1, 2), b(3, 4);
a = b; // 调用 operator=
a += b; // 调用 operator+=

特殊运算符重载

下标运算符 []

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Array{
int data[100];
int size;

Array() : size(0) {
for(int i = 0; i < 100; i++) data[i] = 0;
}

// 重载下标: x[y] -> (Array, int)
int& operator[](int index) {
return this->data[index]; // 返回引用,可以修改
}

// const版本,只读
int operator[](int index) const {
return this->data[index]; // 返回值,不能修改
}
};

// 使用示例:
Array arr;
arr[0] = 10; // 调用 operator[]
int val = arr[0]; // 调用 const version

函数调用运算符 ()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Adder{
int base;

Adder(int b = 0) : base(b) {}

// 重载函数调用: x(y) -> (Adder, 参数...)
int operator()(int value) const {
return this->base + value;
}

// 可以有多个参数
int operator()(int a, int b) const {
return this->base + a + b;
}
};

// 使用示例:
Adder add5(5);
int result1 = add5(10); // 调用 operator()(int),结果是15
int result2 = add5(10, 20); // 调用 operator()(int, int),结果是35

箭头运算符 ->

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct SmartPointer{
int* ptr;

SmartPointer(int* p = nullptr) : ptr(p) {}

// 重载箭头: x->y -> (SmartPointer)
int* operator->() {
return this->ptr;
}

// 解引用: *x -> (SmartPointer)
int& operator*() {
return *(this->ptr);
}

~SmartPointer() {
delete ptr;
}
};

// 使用示例:
int* raw_ptr = new int(42);
SmartPointer smart_ptr(raw_ptr);
cout << *smart_ptr << endl; // 调用 operator*
cout << smart_ptr->operator int() << endl; // 访问指针指向的内容

类型转换运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Point{
int x, y;

Point(int x = 0, int y = 0) : x(x), y(y) {}

// 转换为int: (int)x -> (Point) [注意:参数为空]
operator int() const {
return this->x + this->y; // 返回x+y
}

// 转换为double: (double)x -> (Point)
operator double() const {
return sqrt(this->x * this->x + this->y * this->y); // 返回距离原点的距离
}
};

// 使用示例:
Point p(3, 4);
int sum = p; // 自动转换为int,结果是7
double dist = p; // 自动转换为double,结果是5.0

完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;

struct Complex{
double real, imag;

// 构造函数
Complex(double r = 0, double i = 0) : real(r), imag(i) {}

// 二元加法: x+y -> (Complex, Complex)
Complex operator+(const Complex& other) const {
return Complex(this->real + other.real, this->imag + other.imag);
}

// 二元乘法: x*y -> (Complex, Complex)
Complex operator*(const Complex& other) const {
return Complex(
this->real * other.real - this->imag * other.imag,
this->real * other.imag + this->imag * other.real
);
}

// 比较相等: x==y -> (Complex, Complex)
bool operator==(const Complex& other) const {
return this->real == other.real && this->imag == other.imag;
}

// 一元负号: -x -> (Complex)
Complex operator-() const {
return Complex(-this->real, -this->imag);
}

// 赋值运算符: x=y -> (Complex, Complex)
Complex& operator=(const Complex& other) {
if(this != &other) {
this->real = other.real;
this->imag = other.imag;
}
return *this;
}

// 类型转换为double: (double)x -> (Complex)
operator double() const {
return sqrt(this->real * this->real + this->imag * this->imag);
}

// 输出函数(不是运算符重载,但有用)
void print() const {
cout << this->real << "+" << this->imag << "i" << endl;
}
};

int main(){
Complex a(3, 4), b(1, 2);

Complex c = a + b; // 使用 operator+
c.print(); // 输出: 4+6i

Complex d = a * b; // 使用 operator*
d.print(); // 输出: -5+10i

Complex e = -a; // 使用 operator-
e.print(); // 输出: -3+-4i

double mag = a; // 使用 operator double()
cout << "Magnitude: " << mag << endl; // 输出: 5

return 0;
}

一些话

这里使用了 AI 生成,因为运算符重载是在是太太太太太太太多了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!