1.值得思考的问题

下面的代码有没有区别?为什么?
i++; //i作为返回值,再i+1
++i; // i自增1,再返回i

由于编译器的优化,i++和++i的底层代码都是一样的,所以在工程上没有区别

汇编的结果:
i++;
000920DC mov eax,dword ptr [i]
000920DF add eax,1
000920E2 mov dword ptr [i],eax
++i;
000920E5 mov eax,dword ptr [i]
000920E8 add eax,1
000920EB mov dword ptr [i],eax

意想不到的事实:
现代编译器产品会对代码进行优化
优化使得最终的二进制程序更加高效
优化后的二进制程序丢失了c/c++的原生语义
不可能从编译后的二进制程序还原c/c++程序

思考:++操作符可以重载吗?如何区分前置和后置++????

2.++操作符重载

++操作符可以被重载
—-全局函数和成员函数均可进行重载
—-重载前置++操作符不需要额外的参数
—-重载后置++操作符需要一个int类型的占位参数

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Test
  5. {
  6. int mValue;
  7. public:
  8. Test(int i)
  9. {
  10. mValue = i;
  11. }
  12. int value()
  13. {
  14. return mValue;
  15. }
  16. Test& operator ++ () //返回的是自身
  17. {
  18. ++(this->mValue);
  19. return *this;
  20. }
  21. Test operator ++ (int) //返回值是对象,遵循c语言中后置操作符的原生语义
  22. {
  23. Test ret(this->mValue); //将当前对象的值保存
  24. this->mValue++;
  25. return ret;
  26. }
  27. };
  28. int main()
  29. {
  30. Test t0(0);
  31. cout << t0.value() << endl; //0
  32. Test tt = ++t0;
  33. cout << t0.value() << endl; //1
  34. cout << tt.value() << endl; //1
  35. Test tx = t0++;
  36. cout << tx.value() << endl; //1
  37. cout << t0.value() << endl; //2
  38. Test t;
  39. ++t;
  40. t++; //两者不一样,前置的++效率更高
  41. return 0;
  42. }

所以:i++和++i有没有区别?
对于基础类型的变量:
—-前置++的效率与后置++的效率基本相同
—-根据项目编码规范进行选择

对于类类型对象
—-前置++的效率高于后置++
—-尽力使用前置++操作符提供程序的效率

完善的复数类:

  1. #ifndef _COMPLEX_H_
  2. #define _COMPLEX_H_
  3. class Complex
  4. {
  5. double a;
  6. double b;
  7. public:
  8. Complex(double a = 0,double b = 0);
  9. double getA();
  10. double getB();
  11. double getModulus();
  12. Complex operator +(const Complex& c);
  13. Complex operator -(const Complex& c);
  14. Complex operator *(const Complex& c);
  15. Complex operator /(const Complex& c);
  16. bool operator ==(const Complex& c);
  17. bool operator !=(const Complex& c);
  18. Complex& operator =(const Complex& c);
  19. Complex& operator ++();
  20. Complex operator ++(int);
  21. };
  22. #endif
  23. #include"complex.h"
  24. #include"math.h"
  25. Complex::Complex(double a,double b)//default only exist at deleration
  26. {
  27. this->a = a;
  28. this->b = b;
  29. }
  30. double Complex::getA()
  31. {
  32. return a;
  33. }
  34. double Complex::getB()
  35. {
  36. return b;
  37. }
  38. double Complex::getModulus()
  39. {
  40. return sqrt(a*a + b*b);
  41. }
  42. Complex Complex::operator +(const Complex& c)
  43. {
  44. double na = this->a + c.a;
  45. double nb = this->b + c.b;
  46. Complex ret(na,nb);
  47. return ret;
  48. }
  49. Complex Complex::operator -(const Complex& c)
  50. {
  51. double na = this->a - c.a;
  52. double nb = this->b - c.b;
  53. Complex ret(na,nb);
  54. return ret;
  55. }
  56. Complex Complex::operator *(const Complex& c)
  57. {
  58. double na = (this->a * c.a) - (this->b * c.b);
  59. double nb = (this->a * c.b) + (this->b * c.a);
  60. Complex ret(na,nb);
  61. return ret;
  62. }
  63. Complex Complex::operator /(const Complex& c)
  64. {
  65. double cm = c.a * c.a + c.b* c.b;
  66. double na = (this->a * c.a) + (this->b * c.b) / cm;
  67. double nb = (this->b * c.a) - (this->a * c.b) / cm;
  68. Complex ret(na,nb);
  69. return ret;
  70. }
  71. bool Complex::operator ==(const Complex& c)
  72. {
  73. return (this->a == c.a)&& (this->b == c.b);
  74. }
  75. bool Complex::operator !=(const Complex& c)
  76. {
  77. return !(*this == c);
  78. }
  79. Complex& Complex::operator =(const Complex& c)
  80. {
  81. if(this !=&c)
  82. {
  83. this->a = c.a;
  84. this->b = c.b;
  85. }
  86. return *this; //return current
  87. }
  88. Complex& Complex::operator ++()
  89. {
  90. this->a = this->a + 1;
  91. this->b = this->b + 1;
  92. return *this;
  93. }
  94. Complex Complex::operator ++(int)
  95. {
  96. Complex ret(a,b);
  97. this->a = this->a + 1;
  98. this->b = this->b + 1;
  99. return ret;
  100. }
  101. //main.c
  102. #include <iostream>
  103. #include <string>
  104. #include "complex.h"
  105. using namespace std;
  106. int main()
  107. {
  108. Complex c(0,0);
  109. Complex b = c++;
  110. cout << c.getA() << endl;
  111. cout << c.getB() << endl;
  112. cout << b.getA() << endl;
  113. cout << b.getB() << endl;
  114. return 0;
  115. }

小结:

1.编译优化使得最终的可执行程序更加高效
2.前置++操作符和后置++操作符都可以被重载
3.++操作符的重载必须符合其原生语义
4.对于基础类型,前置++与后置++的效率几乎相同
5.对于类类型,前置++的效率高于后置++