第30课 - 操作符重载的概念
1. 需要解决的问题
下面的复数解决方案是否可行? 不可行,+ 操作符只提供对C++ 基本数据类型的运算,不支持类的相加运算。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 Complex c1(1, 2); 40 Complex c2(3, 4); 41 Complex c3 = Add(c1, c2); // c1 + c2 42 43 printf("c3.a = %d, c3.b = %d ", c3.getA(), c3.getB()); 44 45 return 0; 46 }
思考:Add 函数可以解决Complex 对象相加的问题,但是 Comples 是现实世界中确实存在的复数,并且复数在数学中的地位和普通的实数相同。
为什么不让 + 操作符也支持复数相加呢?
2. 操作符的重载
2.1 操作符重载的概念
(1)C++ 中的重载能够扩展操作符的功能
(2)操作符的重载以函数的方式进行
(3)本质:用特殊形式的函数扩展操作符的功能
2.2 operator关键字
(1)通过 operator 关键字可以定义特殊的函数
(2)operator 的本质是通过函数重载操作符
(3)语法:
Type operator sign(const Type& p1, const Type& p2) { Type ret; return ret; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 3 class Complex 4 { 5 private: 6 int a; 7 int b; 8 public: 9 Complex(int a = 0, int b = 0) 10 { 11 this->a = a; 12 this->b = b; 13 } 14 15 int getA() 16 { 17 return a; 18 } 19 20 int getB() 21 { 22 return b; 23 } 24 25 friend Complex operator +(Complex& p1, Complex& p2); 26 }; 27 28 Complex operator +(Complex& p1, Complex& p2) 29 { 30 Complex ret; 31 32 ret.a = p1.a + p2.a; 33 ret.b = p1.b + p2.b; 34 35 return ret; 36 } 37 38 int main(void) 39 { 40 Complex c1(1, 2); 41 Complex c2(3, 4); 42 43 Complex c3 = c1 + c2; // operator +(c1, c2) 44 45 printf("c3.a = %d, c3.b = %d ", c3.getA(), c3.getB()); 46 47 return 0; 48 }
2.3 将操作符重载函数定义为类的成员函数
(1)比全局操作符重载函数少一个参数(左操作数,类的内部,由this指针完成)
(2)不需要依赖友元就可以完成操作符重载
(3)编译器优先在成员函数中寻找操作符重载函数
class Type { public: Type operator Sign(const Type& p) { Type ret; return ret; } };
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 3 class Complex 4 { 5 private: 6 int a; 7 int b; 8 public: 9 Complex(int a = 0, int b = 0) 10 { 11 this->a = a; 12 this->b = b; 13 } 14 15 int getA() 16 { 17 return a; 18 } 19 20 int getB() 21 { 22 return b; 23 } 24 25 Complex operator + (const Complex& p) 26 { 27 Complex ret; 28 printf("Complex operator + (const Complex& p) "); 29 ret.a = this->a + p.a; 30 ret.b = this->b + p.b; 31 32 return ret; 33 } 34 friend Complex operator + (const Complex& p1, const Complex& p2); 35 }; 36 37 38 Complex operator + (const Complex& p1, const Complex& p2) 39 { 40 Complex ret; 41 printf("Complex operator + (const Complex& p1, const Complex& p2) "); 42 ret.a = p1.a + p2.a; 43 ret.b = p1.b + p2.b; 44 45 return ret; 46 } 47 48 49 int main(void) 50 { 51 Complex c1(1, 2); 52 Complex c2(3, 4); 53 54 Complex c3 = c1 + c2; 55 56 printf("c3.a = %d, c3.b = %d ", c3.getA(), c3.getB()); 57 58 return 0; 59 }
3. 小结
(1)操作符重载是C++ 的强大特性之一
(2)操作符重载的本质是通过函数扩展操作符的功能
(3)operator 关键字是实现操作符重载的关键
(4)操作符重载遵循相同的函数重载规则
(5)全局函数和成员函数都可以实现对操作符的重载