溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++如何實現(xiàn)操作符重載

發(fā)布時間:2021-12-01 09:09:34 來源:億速云 閱讀:145 作者:小新 欄目:大數(shù)據(jù)

這篇文章主要介紹了C++如何實現(xiàn)操作符重載,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。


在C++中經(jīng)常會遇到重載運算符的問題,其實運算符重載必須將運算符看做
一個函數(shù),分清他的形參返回值,必須搞清楚內(nèi)存在哪里如何分配如何回收
什么時候生成匿名對象,什么時候使用this指針返回。
運算符可以用友元函數(shù)和成員函數(shù)完成,一般來講都使用成員函數(shù),但是某些
特殊的情況必須使用友元函數(shù),比如<< 因為其左操作數(shù)為ostream&類型,是不
可能進行任何修改的。

成員函數(shù)運算符重載大體步驟如下:

比如Tclass a
    Tclass b
我們要重載=號
    a = b
1、將需要重載的運算符看做一個函數(shù)生成operator函數(shù)名
如重載等待=
即operator=
2、分清楚形參
   如果是等號重載很明顯如果是成員函數(shù)形參是右操作數(shù)b原型為及
   Tclass& b
   這個時候第一操作數(shù)被隱藏即 *this。
3、分清返回值
為了實現(xiàn)如:a=b=c的級聯(lián)編程,因為=連接性右到左
則為
a=(b=c)即 a.operator=(b.operator=(c))
那么b=c需要返回一個Tclass&類型,當然最好就是 b直接返回
也就是*this內(nèi)存空間。
那么分析到這里我們可以寫出函數(shù)原型和返回如下:


Tclass& operator=( Tclass& b)
{
...........
return *this;
}
具體實現(xiàn)具體分析。


下面是一個關(guān)于char*類型類的運算符重載,包括了
1、=操作符重載(深拷貝)
2、+操作符重載
3、前置++ 后置++重載
4、!= ==重載
5、<< 重載
因為涉及到char*類型的類容易引起內(nèi)存泄露下面是測試程序內(nèi)存泄露檢查
==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<iostream>

  8. #include<stdlib.h>

  9. #include<string.h>


  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 構(gòu)造函數(shù)深拷貝

  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<<this->mystr<<endl;

  39.                 }


  40.                 ~testop()

  41.                 {

  42.                         delete [] this->mystr;

  43.                 }

  44.                 //操作符重載 + 使用成員函數(shù)

  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.                 //操作符重載 = 使用成員函數(shù) 深拷貝

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

  56.                 {

  57.                         if(this->mystr != NULL)//必須先釋放內(nèi)存

  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.                 //操作符重載前置(++),使用成員函數(shù) 支持鏈試編程


  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.                 //操作符重載后置(++),使用成員函數(shù) 不支持鏈試編程 因為返回的為一個匿名對象


  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.                 //操作符重載 << 必須使用友元函數(shù) 支持鏈試編程

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

  96.                 //操作符重載 == 使用成員函數(shù)

  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.                 //操作符重載 != 使用成員函數(shù)

  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) // 友元函數(shù)

  122. {

  123.         out<<b.mystr;

  124.         return out;

  125. }





  126. int main()

  127. {

  128.         testop c("ab");

  129.         cout<<c<<endl;

  130.         c++;

  131.         ++c;

  132.         cout<<"c:"<<c<<endl;

  133.         testop a=c;

  134.         cout<<"a:"<<a<<endl;

  135.         if(a == c)

  136.         {

  137.                 cout<<"相等"<<endl;

  138.         }

  139.         a = c+a;

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

  141.         if(a !=c )

  142.         {

  143.                 cout<<"不相等"<<endl;

  144.         }


  145. }



結(jié)果如下:
gaopeng@bogon:~/cplusnew/操作符重載$ ./a.out 
ab
c:abababab
a:abababab
相等
a=c+a:abababababababab
不相等

感謝你能夠認真閱讀完這篇文章,希望小編分享的“C++如何實現(xiàn)操作符重載”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI