千家信息网

C++如何实现操作符重载

发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,这篇文章主要介绍了C++如何实现操作符重载,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。在C++中经常会遇到重载运算符的问题,其实运算
千家信息网最后更新 2025年02月04日C++如何实现操作符重载

这篇文章主要介绍了C++如何实现操作符重载,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。


在C++中经常会遇到重载运算符的问题,其实运算符重载必须将运算符看做
一个函数,分清他的形参返回值,必须搞清楚内存在哪里如何分配如何回收
什么时候生成匿名对象,什么时候使用this指针返回。
运算符可以用友元函数和成员函数完成,一般来讲都使用成员函数,但是某些
特殊的情况必须使用友元函数,比如<< 因为其左操作数为ostream&类型,是不
可能进行任何修改的。

成员函数运算符重载大体步骤如下:

比如Tclass a
Tclass b
我们要重载=号
a = b
1、将需要重载的运算符看做一个函数生成operator函数名
如重载等待=
即operator=
2、分清楚形参
如果是等号重载很明显如果是成员函数形参是右操作数b原型为及
Tclass& b
这个时候第一操作数被隐藏即 *this。
3、分清返回值
为了实现如:a=b=c的级联编程,因为=连接性右到左
则为
a=(b=c)即 a.operator=(b.operator=(c))
那么b=c需要返回一个Tclass&类型,当然最好就是 b直接返回
也就是*this内存空间。
那么分析到这里我们可以写出函数原型和返回如下:


Tclass& operator=( Tclass& b)
{
...........
return *this;
}
具体实现具体分析。


下面是一个关于char*类型类的运算符重载,包括了
1、=操作符重载(深拷贝)
2、+操作符重载
3、前置++ 后置++重载
4、!= ==重载
5、<< 重载
因为涉及到char*类型的类容易引起内存泄露下面是测试程序内存泄露检查
==5613== HEAP SUMMARY:
==5613== in use at exit: 0 bytes in 0 blocks
==5613== total heap usage: 9 allocs, 9 frees, 102 bytes allocated
==5613==
==5613== All heap blocks were freed -- no leaks are possible
==5613==
==5613== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==5613== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)


具体代码如下:


点击(此处)折叠或打开

  1. /*************************************************************************

  2. > File Name: class.cpp

  3. > Author: gaopeng QQ:22389860 all right reserved

  4. > Mail: gaopp_200217@163.com

  5. > Created Time: Sat 25 Mar 2017 04:40:31 PM CST

  6. ************************************************************************/


  7. #include

  8. #include

  9. #include


  10. using namespace std;



  11. class testop

  12. {

  13. private:

  14. char* mystr;

  15. int len;

  16. public:

  17. testop(const char* instr)

  18. {

  19. this->len = strlen(instr)+1;

  20. this->mystr = new char[this->len];

  21. memset(this->mystr,0,this->len);

  22. strcpy(this->mystr,instr);

  23. }

  24. testop()

  25. {

  26. this->len = 0;

  27. this->mystr = NULL;

  28. }

  29. testop(const testop& b)//copy 构造函数深拷贝

  30. {

  31. this->len = b.len;

  32. this->mystr = new char[b.len];

  33. memset(this->mystr,0,this->len);

  34. strcpy(this->mystr,b.mystr);

  35. }

  36. void printmystr()

  37. {

  38. cout<mystr<

  39. }


  40. ~testop()

  41. {

  42. delete [] this->mystr;

  43. }

  44. //操作符重载 + 使用成员函数

  45. testop operator+(const testop& b)

  46. {

  47. testop temp;

  48. temp.len = this->len + b.len;

  49. temp.mystr = new char[temp.len];

  50. memset(temp.mystr,0,temp.len);

  51. strcat(strcat(temp.mystr,this->mystr),b.mystr);

  52. return temp;

  53. }

  54. //操作符重载 = 使用成员函数 深拷贝

  55. testop& operator=(const testop& b)

  56. {

  57. if(this->mystr != NULL)//必须先释放内存

  58. {

  59. delete [] this->mystr;

  60. }

  61. this->len = b.len;

  62. this->mystr = new char[this->len];

  63. memset(this->mystr,0,this->len);

  64. strcpy(this->mystr,b.mystr);

  65. return *this;

  66. }

  67. //操作符重载前置(++),使用成员函数 支持链试编程


  68. testop& operator++()

  69. {

  70. this->len = this->len+this->len;

  71. char* temp = new char[this->len];

  72. memset(temp,0,this->len);

  73. strcat(strcat(temp,this->mystr),this->mystr);

  74. delete [] this->mystr;

  75. this->mystr = new char[this->len];

  76. strcpy(this->mystr,temp);

  77. delete [] temp;

  78. return *this;

  79. }


  80. //操作符重载后置(++),使用成员函数 不支持链试编程 因为返回的为一个匿名对象


  81. testop operator++(int)

  82. {

  83. testop tempop = *this;

  84. this->len = this->len+this->len;

  85. char* temp = new char[this->len];

  86. memset(temp,0,this->len);

  87. strcat(strcat(temp,this->mystr),this->mystr);

  88. delete [] this->mystr;

  89. this->mystr = new char[this->len];

  90. strcpy(this->mystr,temp);

  91. delete [] temp;

  92. return tempop;

  93. }



  94. //操作符重载 << 必须使用友元函数 支持链试编程

  95. friend ostream& operator<<(ostream& out,testop& b);

  96. //操作符重载 == 使用成员函数

  97. bool operator==(testop& b)

  98. {

  99. if(this->len == b.len && !strcmp(this->mystr,b.mystr))

  100. {

  101. return true;

  102. }

  103. else

  104. {

  105. return false;

  106. }

  107. }

  108. //操作符重载 != 使用成员函数

  109. bool operator!=(testop& b)

  110. {

  111. if((*this) == b )

  112. {

  113. return false;

  114. }

  115. else

  116. {

  117. return true;

  118. }

  119. }


  120. };


  121. ostream& operator<<(ostream& out,testop& b) // 友元函数

  122. {

  123. out<

  124. return out;

  125. }





  126. int main()

  127. {

  128. testop c("ab");

  129. cout<

  130. c++;

  131. ++c;

  132. cout<<"c:"<

  133. testop a=c;

  134. cout<<"a:"<

  135. if(a == c)

  136. {

  137. cout<<"相等"<

  138. }

  139. a = c+a;

  140. cout<<"a=c+a:"<

  141. if(a !=c )

  142. {

  143. cout<<"不相等"<

  144. }


  145. }



结果如下:
gaopeng@bogon:~/cplusnew/操作符重载$ ./a.out
ab
c:abababab
a:abababab
相等
a=c+a:abababababababab
不相等

感谢你能够认真阅读完这篇文章,希望小编分享的"C++如何实现操作符重载"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

0