C++ 运算符重载二(一元运算符重载)

柔情只为你懂 2022-03-28 16:52 422阅读 0赞
  1. //一元运算符重载
  2. #include<iostream>
  3. using namespace std;
  4. class Point
  5. {
  6. public:
  7. Point(int x,int y){
  8. this->x = x;
  9. this->y = y;
  10. }
  11. Point(Point &p){
  12. this->x = p.x;
  13. this->y = p.y;
  14. cout << "拷贝构造函数被执行了1" << endl;
  15. }
  16. ~Point(){
  17. cout << "析构函数被执行了2" << endl;
  18. }
  19. //加法
  20. friend Point operator+(Point p1, Point p2);
  21. //前置++
  22. friend Point& operator++(Point& pin);
  23. //前置--
  24. Point& operator--(){
  25. this->x--;
  26. this->y--;
  27. return *this;
  28. }
  29. //后置++
  30. friend Point operator++(Point& pin, int);
  31. //后置--
  32. Point operator--(int){
  33. Point temp = *this;
  34. this->x--;
  35. this->y--;
  36. return temp;
  37. }
  38. void PrintfA(){
  39. cout << "x=" << this->x << endl;
  40. cout << "y=" << this->y << endl;
  41. }
  42. private:
  43. int x;
  44. int y;
  45. };
  46. //+
  47. Point operator+(Point p1, Point p2){
  48. Point pres(p1.x + p2.x, p1.y + p2.y);
  49. return pres;
  50. }
  51. //前置++
  52. Point& operator++(Point& pin){
  53. pin.x++;
  54. pin.y++;
  55. //注明此处不存在pin的构造,析构 因为pin这个对象一直存在
  56. return pin;
  57. }
  58. //后置++
  59. //Point & operator++(Point& pin, int)多了一个占位int类型参数表示是后置运算符
  60. //这是c++的语法,这个占位参数只能是Int类型
  61. Point operator++(Point& pin, int){
  62. Point temp = pin;
  63. //这里使用临时变量是为了保存后置++之前的状态
  64. //因为后置++参加完运算后才会自增
  65. pin.x++;
  66. pin.y++;
  67. return temp;
  68. }
  69. void ProtectA(){
  70. Point p1(5, 5);
  71. //前置++ (先自身++,在进行运算)
  72. //全局函数 运算符重载
  73. ++p1;
  74. //步骤1:首先承认运算符重载是一个函数
  75. //operator++()
  76. //步骤2:根据操作数,写出参数列表
  77. //operator++(p1)
  78. //步骤3:根据业务完成函数返回值,以及实现函数
  79. //Point& operator++(Point& pin);
  80. //此处的返回值为啥不是void呢?
  81. //前置++是自身变量的++(即对象本身的自增),我传递的参数是引用,改变的就是对象本身,为啥还要将对象返回出来呢?
  82. //首先明确一点 运算符重载是一个函数,假设 p1+(++p2) 如果返回NULL 那么p1+这个运算就会有问题
  83. p1.PrintfA();
  84. //类的成员函数 运算符重载
  85. //前置--
  86. --p1;
  87. //步骤1:首先承认运算符重载是一个函数
  88. //operator--()
  89. //步骤2:根据操作数,写出参数列表
  90. //p1.operator--()
  91. //步骤3:根据业务完成函数返回值,以及实现函数
  92. //Point& operator--();
  93. p1.PrintfA();
  94. //全局函数 运算符重载
  95. //后置++(后置++ 必须运算完成之后,自身才可以自增)
  96. //p1++;
  97. //步骤1:首先承认运算符重载是一个函数
  98. //operator++()
  99. //步骤2:根据操作数,写出参数列表
  100. //operator++(Point& pin,int)
  101. //步骤3:根据业务完成函数返回值,以及实现函数
  102. //Point operator++(Point& pin, int)
  103. //特别注意:这里后置++的返回值必须是Point对象 不可以是引用
  104. //因为如果是引用,返回值是一个临时变量,执行return temp;之后这个临时变量会被销毁
  105. //引用的话会继续指向这个被销毁的临时变量,出现脏数据、
  106. //但是返回是Point(匿名对象)就会不一样,执行return temp;之后,c++编译器生成一个匿名对象
  107. //把临时变量拷贝到匿名对象中,执行Point p3 = p1++;,匿名对象会直接转化成p3,二不会被销毁
  108. //这样的结果才是正确的,因此我们需要改变operator+(Point p1, Point p2)函数,参数类型只能是Point
  109. //而不可以是Point &引用;因为p1+p2的返回值应该是一个临时变量,而不能改变p1或者p2本身的值
  110. //当然我并不是说operator+(Point &p1, Point &p2)重载+号就是错误,但是在本场景下,不能使用引用
  111. Point p2(2, 2);
  112. Point p3 = p1++;
  113. Point p4 = p1 + (p2++);
  114. cout << "p4-------------------" << endl;
  115. p4.PrintfA();
  116. cout << "p2-------------------" << endl;
  117. p2.PrintfA();
  118. //类的成员函数 运算符重载
  119. //后置--
  120. cout << "后置-- p1的原先值" << endl;
  121. p1.PrintfA();
  122. p1--;
  123. cout << "后置-- p1的结果值" << endl;
  124. p1.PrintfA();
  125. //步骤1:首先承认运算符重载是一个函数
  126. //operator--()
  127. //步骤2:根据操作数,写出参数列表
  128. //operator--(int)
  129. //步骤3:根据业务完成函数返回值,以及实现函数
  130. //Point operator--(int)
  131. }
  132. void main(){
  133. ProtectA();
  134. system("pause");
  135. }

发表评论

表情:
评论列表 (有 0 条评论,422人围观)

还没有评论,来说两句吧...

相关阅读