操作符重载的概念

复数计算第一种形式(自定义复数类):

 1 #include <stdio.h>
 2 
 3 class Complex 
 4 {
 5     int a;
 6     int b;
 7 public:
 8     Complex(int a = 0, int b = 0)
 9     {
10         this->a = a;
11         this->b = b;
12     }
13     
14     int getA()
15     {
16         return a;
17     }
18     
19     int getB()
20     {
21         return b;
22     }
23     
24     friend Complex Add(const Complex& p1, const Complex& p2);
25 };
26 
27 Complex Add(const Complex& p1, const Complex& p2)
28 {
29     Complex ret;
30     
31     ret.a = p1.a + p2.a;
32     ret.b = p1.b + p2.b;
33     
34     return ret;
35 }
36 
37 int main()
38 {
39 
40     Complex c1(1, 2);
41     Complex c2(3, 4);
42     Complex c3 = Add(c1, c2); // c1 + c2
43     
44     printf("c3.a = %d, c3.b = %d
", c3.getA(), c3.getB());
45     
46     return 0;
47 }

Question:

Add 函数可以解决Complex对象相加的问题;

但是Complex是现实世界的确定存在的复数;

并且复数的地位在数学中的地位与普通的实数地位i相同;

为什么不能+操作符也支持复数相加呢?

操作符重载:

C++中的operator关键字可以定义特殊的函数

operator的本质是通过函数重载操作符;

 1 #include <stdio.h>
 2 
 3 class Complex 
 4 {
 5     int a;
 6     int b;
 7 public:
 8     Complex(int a = 0, int b = 0)
 9     {
10         this->a = a;
11         this->b = b;
12     }
13     
14     int getA()
15     {
16         return a;
17     }
18     
19     int getB()
20     {
21         return b;
22     }
23     
24     friend Complex operator + (const Complex& p1, const Complex& p2);
25 };
26 
27 Complex operator + (const Complex& p1, const Complex& p2)
28 {
29     Complex ret;
30     
31     ret.a = p1.a + p2.a;
32     ret.b = p1.b + p2.b;
33     
34     return ret;
35 }
36 
37 int main()
38 {
39 
40     Complex c1(1, 2);
41     Complex c2(3, 4);
42     Complex c3 = c1 + c2; // operator + (c1, c2)
43     
44     printf("c3.a = %d, c3.b = %d
", c3.getA(), c3.getB());
45     
46     return 0;
47 }

操作符重载的本质就是函数重载.

那还有什么深度挖掘的地方呢?上面的例子我们使用了友元,而友元是我们尽量避免的,那我们可以怎样修改呢?

-可以将操作符重载函数定义为类中的  成员函数.

  -比全局操作符重载函数少一个参数(左操作数)

  -不需要依赖友元就可以完成操作符重载;

  -编译器优先在成员函数中寻找操作符操作函数;

实例代码分析3:

 1 #include <stdio.h>
 2 
 3 class Complex 
 4 {
 5     int a;
 6     int b;
 7 public:
 8     Complex(int a = 0, int b = 0)
 9     {
10         this->a = a;
11         this->b = b;
12     }
13     
14     int getA()
15     {
16         return a;
17     }
18     
19     int getB()
20     {
21         return b;
22     }
23     
24     Complex operator + (const Complex& p)
25     {
26         Complex ret;
27         printf("Complex operator + (const Complex& p)
");
28         ret.a = this->a + p.a;
29         ret.b = this->b + p.b;
30         
31         return ret;
32     }
33     
34     friend Complex operator + (const Complex& p1, const Complex& p2);
35 };
36 
37 Complex operator + (const Complex& p1, const Complex& p2)
38 {
39     Complex ret;
40     printf("Complex operator + (const Complex& p1, const Complex& p2)
");
41     ret.a = p1.a + p2.a;
42     ret.b = p1.b + p2.b;
43     
44     return ret;
45 }
46 
47 int main()
48 {
49 
50     Complex c1(1, 2);
51     Complex c2(3, 4);
52     Complex c3 = c1 + c2; // c1.operator + (c2)
53     
54     printf("c3.a = %d, c3.b = %d
", c3.getA(), c3.getB());
55     
56     return 0;
57 }

小结:

操作符重载是C++的强大特性之一;

操作符重载的本质是通过函数扩展操作符的功能;

operator关键字是实现操作符重载的关键

操作符重载遵循相同的函数重载规则

全局函数成员函数都可以实现对操作符的重载.

原文地址:https://www.cnblogs.com/lemaden/p/10118209.html