运算符重载基本概念

运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。

运算符重载(operator overloading)只是一种语法上的方便,也就是它只是另一种函数调用的方式。

在c++中,可以定义一个处理类的新运算符。这种定义很像一个普通的函数定义,只是函数的名字由关键字operator及其紧跟的运算符组成。差别仅此而已。它像任何其他函数一样也是一个函数,当编译器遇到适当的模式时,就会调用这个函数。
语法
定义重载的运算符就像定义函数,只是该函数的名字是operator@,这里的@代表了被重载的运算符。函数的参数中参数个数取决于两个因素。

  • 运算符是一元(一个参数)的还是二元(两个参数);
  • 运算符被定义为全局函数(对于一元是一个参数,对于二元是两个参数)还是成员函数(对于一元没有参数,对于二元是一个参数-此时该类的对象用作左耳参数)

运算符重载碰上友元函数

友元函数是一个全局函数,和我们上例写的全局函数类似,只是友元函数可以访问某个类私有数据。

  1. // 重载左移操作符(<<),使得cout可以输出对象。
  2. class Person{
  3. friend ostream& operator<<(ostream& os, Person& person);
  4. public:
  5. Person(int id,int age){
  6. mID = id;
  7. mAge = age;
  8. }
  9. private:
  10. int mID;
  11. int mAge;
  12. };
  13. ostream& operator<<(ostream& os, Person& person){
  14. os << "ID:" << person.mID << " Age:" << person.mAge;
  15. return os;
  16. }
  17. int main(){
  18. Person person(1001, 30);
  19. //cout << person; //cout.operator+(person)
  20. cout << person << " | " << endl;
  21. return EXIT_SUCCESS;
  22. }

可重载的运算符

几乎C中所有的运算符都可以重载,但运算符重载的使用时相当受限制的。特别是不能使用C中当前没有意义的运算符(例如用**求幂)不能改变运算符优先级,不能改变运算符的参数个数。这样的限制有意义,否则,所有这些行为产生的运算符只会混淆而不是澄清寓语意。
image.png

自增自减(++/—)运算符重载

重载的++和--运算符有点让人不知所措,因为我们总是希望能根据它们出现在所作用对象的前面还是后面来调用不同的函数。解决办法很简单,例如当编译器看到++a(前置++),它就调用operator++(a),当编译器看到a++(后置++),它就会去调用operator++(a,int)。

  1. class Complex{
  2. friend ostream& operator<<(ostream& os,Complex& complex){
  3. os << "A:" << complex.mA << " B:" << complex.mB << endl;
  4. return os;
  5. }
  6. public:
  7. Complex(){
  8. mA = 0;
  9. mB = 0;
  10. }
  11. //重载前置++
  12. Complex& operator++(){
  13. mA++;
  14. mB++;
  15. return *this;
  16. }
  17. //重载后置++
  18. Complex operator++(int){
  19. Complex temp;
  20. temp.mA = this->mA;
  21. temp.mB = this->mB;
  22. mA++;
  23. mB++;
  24. return temp;
  25. }
  26. //前置--
  27. Complex& operator--(){
  28. mA--;
  29. mB--;
  30. return *this;
  31. }
  32. //后置--
  33. Complex operator--(int){
  34. Complex temp;
  35. temp.mA = mA;
  36. temp.mB = mB;
  37. mA--;
  38. mB--;
  39. return temp;
  40. }
  41. void ShowComplex(){
  42. cout << "A:" << mA << " B:" << mB << endl;
  43. }
  44. private:
  45. int mA;
  46. int mB;
  47. };
  48. void test(){
  49. Complex complex;
  50. complex++;
  51. cout << complex;
  52. ++complex;
  53. cout << complex;
  54. Complex ret = complex++;
  55. cout << ret;
  56. cout << complex;
  57. cout << "------" << endl;
  58. ret--;
  59. --ret;
  60. cout << "ret:" << ret;
  61. complex--;
  62. --complex;
  63. cout << "complex:" << complex;
  64. }

指针运算符(*、->)重载

  1. class Person{
  2. public:
  3. Person(int param){
  4. this->mParam = param;
  5. }
  6. void PrintPerson(){
  7. cout << "Param:" << mParam << endl;
  8. }
  9. private:
  10. int mParam;
  11. };
  12. class SmartPointer{
  13. public:
  14. SmartPointer(Person* person){
  15. this->pPerson = person;
  16. }
  17. //重载指针的->、*操作符
  18. Person* operator->(){
  19. return pPerson;
  20. }
  21. Person& operator*(){
  22. return *pPerson;
  23. }
  24. ~SmartPointer(){
  25. if (pPerson != NULL){
  26. delete pPerson;
  27. }
  28. }
  29. public:
  30. Person* pPerson;
  31. };
  32. void test01(){
  33. //Person* person = new Person(100);
  34. //如果忘记释放,那么就会造成内存泄漏
  35. SmartPointer pointer(new Person(100));
  36. pointer->PrintPerson();
  37. }

赋值(=)运算符重载

赋值符常常初学者的混淆。这是毫无疑问的,因为=在编程中是最基本的运算符,可以进行赋值操作,也能引起拷贝构造函数的调用。

  1. class Person{
  2. friend ostream& operator<<(ostream& os,const Person& person){
  3. os << "ID:" << person.mID << " Age:" << person.mAge << endl;
  4. return os;
  5. }
  6. public:
  7. Person(int id,int age){
  8. this->mID = id;
  9. this->mAge = age;
  10. }
  11. //重载赋值运算符
  12. Person& operator=(const Person& person){
  13. this->mID = person.mID;
  14. this->mAge = person.mAge;
  15. return *this;
  16. }
  17. private:
  18. int mID;
  19. int mAge;
  20. };
  21. //1. =号混淆的地方
  22. void test01(){
  23. Person person1(10, 20);
  24. Person person2 = person1; //调用拷贝构造
  25. //如果一个对象还没有被创建,则必须初始化,也就是调用构造函数
  26. //上述例子由于person2还没有初始化,所以会调用构造函数
  27. //由于person2是从已有的person1来创建的,所以只有一个选择
  28. //就是调用拷贝构造函数
  29. person2 = person1; //调用operator=函数
  30. //由于person2已经创建,不需要再调用构造函数,这时候调用的是重载的赋值运算符
  31. }
  32. //2. 赋值重载案例
  33. void test02(){
  34. Person person1(20, 20);
  35. Person person2(30, 30);
  36. cout << "person1:" << person1;
  37. cout << "person2:" << person2;
  38. person2 = person1;
  39. cout << "person2:" << person2;
  40. }
  41. //常见错误,当准备给两个相同对象赋值时,应该首先检查一下这个对象是否对自身赋值了
  42. //对于本例来讲,无论如何执行这些赋值运算都是无害的,但如果对类的实现进行修改,那么将会出现差异;
  43. //3. 类中指针
  44. class Person2{
  45. friend ostream& operator<<(ostream& os, const Person2& person){
  46. os << "Name:" << person.pName << " ID:" << person.mID << " Age:" << person.mAge << endl;
  47. return os;
  48. }
  49. public:
  50. Person2(char* name,int id, int age){
  51. this->pName = new char[strlen(name) + 1];
  52. strcpy(this->pName, name);
  53. this->mID = id;
  54. this->mAge = age;
  55. }
  56. #if 1
  57. //重载赋值运算符
  58. Person2& operator=(const Person2& person){
  59. //注意:由于当前对象已经创建完毕,那么就有可能pName指向堆内存
  60. //这个时候如果直接赋值,会导致内存没有及时释放
  61. if (this->pName != NULL){
  62. delete[] this->pName;
  63. }
  64. this->pName = new char[strlen(person.pName) + 1];
  65. strcpy(this->pName,person.pName);
  66. this->mID = person.mID;
  67. this->mAge = person.mAge;
  68. return *this;
  69. }
  70. #endif
  71. //析构函数
  72. ~Person2(){
  73. if (this->pName != NULL){
  74. delete[] this->pName;
  75. }
  76. }
  77. private:
  78. char* pName;
  79. int mID;
  80. int mAge;
  81. };
  82. void test03(){
  83. Person2 person1("John",20, 20);
  84. Person2 person2("Edward",30, 30);
  85. cout << "person1:" << person1;
  86. cout << "person2:" << person2;
  87. person2 = person1;
  88. cout << "person2:" << person2;
  89. }

如果没有重载赋值运算符,编译器会自动创建默认的赋值运算符重载函数。行为类似默认拷贝构造,进行简单值拷贝。

等于和不等于(==、!=)运算符重载

  1. class Complex{
  2. public:
  3. Complex(char* name,int id,int age){
  4. this->pName = new char[strlen(name) + 1];
  5. strcpy(this->pName, name);
  6. this->mID = id;
  7. this->mAge = age;
  8. }
  9. //重载==号操作符
  10. bool operator==(const Complex& complex){
  11. if (strcmp(this->pName,complex.pName) == 0 &&
  12. this->mID == complex.mID &&
  13. this->mAge == complex.mAge){
  14. return true;
  15. }
  16. return false;
  17. }
  18. //重载!=操作符
  19. bool operator!=(const Complex& complex){
  20. if (strcmp(this->pName, complex.pName) != 0 ||
  21. this->mID != complex.mID ||
  22. this->mAge != complex.mAge){
  23. return true;
  24. }
  25. return false;
  26. }
  27. ~Complex(){
  28. if (this->pName != NULL){
  29. delete[] this->pName;
  30. }
  31. }
  32. private:
  33. char* pName;
  34. int mID;
  35. int mAge;
  36. };
  37. void test(){
  38. Complex complex1("aaa", 10, 20);
  39. Complex complex2("bbb", 10, 20);
  40. if (complex1 == complex2){ cout << "相等!" << endl; }
  41. if (complex1 != complex2){ cout << "不相等!" << endl; }
  42. }

函数调用符号()重载

  1. class Complex{
  2. public:
  3. int Add(int x,int y){
  4. return x + y;
  5. }
  6. int operator()(int x,int y){
  7. return x + y;
  8. }
  9. };
  10. void test01(){
  11. Complex complex;
  12. cout << complex.Add(10,20) << endl;
  13. //对象当做函数来调用
  14. cout << complex(10, 20) << endl;
  15. }

不要重载&&、||

不能重载operator&& 和 operator|| 的原因是,无法在这两种情况下实现内置操作符的完整语义。说得更具体一些,内置版本版本特殊之处在于:内置版本的&&和||首先计算左边的表达式,如果这完全能够决定结果,就无需计算右边的表达式了—而且能够保证不需要。我们都已经习惯这种方便的特性了。
我们说操作符重载其实是另一种形式的函数调用而已,对于函数调用总是在函数执行之前对所有参数进行求值。

  1. class Complex{
  2. public:
  3. Complex(int flag){
  4. this->flag = flag;
  5. }
  6. Complex& operator+=(Complex& complex){
  7. this->flag = this->flag + complex.flag;
  8. return *this;
  9. }
  10. bool operator&&(Complex& complex){
  11. return this->flag && complex.flag;
  12. }
  13. public:
  14. int flag;
  15. };
  16. int main(){
  17. Complex complex1(0); //flag 0
  18. Complex complex2(1); //flag 1
  19. //原来情况,应该从左往右运算,左边为假,则退出运算,结果为假
  20. //这边却是,先运算(complex1+complex2),导致,complex1的flag变为complex1+complex2的值, complex1.a = 1
  21. // 1 && 1
  22. //complex1.operator&&(complex1.operator+=(complex2))
  23. if (complex1 && (complex1 += complex2)){
  24. cout << "真!" << endl;
  25. }
  26. else{
  27. cout << "假!" << endl;
  28. }
  29. return EXIT_SUCCESS;
  30. }

根据内置&&的执行顺序,我们发现这个案例中执行顺序并不是从左向右,而是先右后左,这就是不满足我们习惯的特性了。由于complex1 += complex2先执行,导致complex1本身发生了变化,初始值是0,现在经过+=运算变成1,1 && 1输出了真。

符号重载总结

  • =,[],()和-> 操作符只能通过成员函数进行重载
  • <<和>>只能通过全局函数配合友元函数进行重载
  • 不要重载 && 和 || 操作符,因为无法实现短路规则

常规建议
image.png