0%

C++ 运算符重载

重载运算符

运算符重载本质是调用函数

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
#include <iostream>
#include <cstring>
using namespace std;

class Point {
friend Point operator + (Point, Point);
int m_x;
int m_y;
public:
Point(int x, int y) : m_x(x), m_y(y) {};
void display() {
cout << "(" << this->m_x << ", " << this->m_y << ")" << endl;
}
};

Point operator + (Point p1, Point p2) {
cout << "**" << endl;
return Point(p1.m_x + p2.m_x, p1.m_y + p2.m_y);
}

int main() {
Point p1(10, 20);
Point p2(20, 30);
p1.display();

Point p3 = p1 + p2;
// 等价于 Point p4 = operator+(p1, p2);
p3.display();
return 0;
}

进一步完善

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Point {
friend Point operator + (const Point&, const Point&);
int m_x;
int m_y;
public:
Point(int x, int y) : m_x(x), m_y(y) {};
void display() {
cout << "(" << this->m_x << ", " << this->m_y << ")" << endl;
}
Point(const Point& point) {
m_x = point.m_x;
m_y = point.m_y;
}
};

Point operator + (const Point &p1, const Point &p2) {
return Point(p1.m_x + p2.m_x, p1.m_y + p2.m_y);
}

利用成员函数实现运算符重载

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
#include <iostream>
using namespace std;

class Point {
//friend Point operator + (const Point&, const Point&);
int m_x, m_y;
public:
Point(int x, int y) : m_x(x), m_y(y) {};
void display() {
cout << "(" << this->m_x << ", " << this->m_y << ")" << endl;
}
Point(const Point& point) {
m_x = point.m_x;
m_y = point.m_y;
}
Point operator + (const Point &point) {
return Point(this->m_x+point.m_x, this->m_y+point.m_y);
}
};



int main() {
Point p1(10, 20);
Point p2(20, 30);
Point p3(30, 40);
Point p4 = p1 + p2 + p3; //等价于 p1.operator+(p2);
p4.display();

return 0;
}

不能给临时值赋值 的实现方法

1
2
int a, b;
(a + b) = 20; //正常情况下会报错,赋给临时变量,马上被销毁,无意义

可以通过在返回值类型前加const就可实现

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
#include <iostream>
using namespace std;

class Point {
//friend Point operator + (const Point&, const Point&);
int m_x, m_y;
public:
Point(int x, int y) : m_x(x), m_y(y) {};
void display() {
cout << "(" << this->m_x << ", " << this->m_y << ")" << endl;
}
Point(const Point& point) {
m_x = point.m_x;
m_y = point.m_y;
}
const Point operator + (const Point &point) { //*******
return Point(this->m_x+point.m_x, this->m_y+point.m_y);
}
const Point operator - (const Point& point) {
return Point(this->m_x - point.m_x, this->m_y - point.m_y);
}
};



int main() {
Point p1(10, 20);
Point p2(20, 30);
Point p3(30, 40);
Point p4 = p1 + p2 + p3; //等价于 p1.operator+(p2);
p4.display();
p4 = p2 - p1;
p4.display();
(p1 + p2) = p3; //不能给临时变量赋值,在返回值类型前加const就可

return 0;
}

但是这样又存在一个问题
因为 const对象 不能调用 非const函数

1
2
Point p4 = p1 + p2 + p3; //报错
//等价于 p1.operator+(p2).operator+(p3)

将代码修改这样即可

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
#include <iostream>
using namespace std;

class Point {
//friend Point operator + (const Point&, const Point&);
int m_x, m_y;
public:
Point(int x, int y) : m_x(x), m_y(y) {};
void display() {
cout << "(" << this->m_x << ", " << this->m_y << ")" << endl;
}
Point(const Point& point) {
m_x = point.m_x;
m_y = point.m_y;
}
const Point operator + (const Point &point) const { //*******
return Point(this->m_x+point.m_x, this->m_y+point.m_y);
}
const Point operator - (const Point& point) const {
return Point(this->m_x - point.m_x, this->m_y - point.m_y);
}
};



int main() {
Point p1(10, 20);
Point p2(20, 30);
Point p3(30, 40);
Point p4 = p1 + p2 + p3; //等价于 p1.operator+(p2);
p4.display();
p4 = p2 - p1;
p4.display();
//(p1 + p2) = p3; //不能给临时变量赋值,在返回值类型前加const就可

return 0;
}

+= 重载运算符

1
2
3
4
5
6
7
8
9
10
......
Point & operator += (const Point& point) { //*******
m_x += point.m_x;
m_y += point.m_y;
return *this;
}
..........

(p1 += p2) = Point(2, 3);
p1.display();

== != 重载运算符

1
2
3
4
5
6
bool operator == (const Point& point) {
return (m_x == point.m_x && m_y == point.m_y);
}
bool operator != (const Point& point) {
return (m_x != point.m_x || m_y != point.m_y);
}
求大佬赏个饭