C++操作符過載

gaopengtttt發表於2017-03-12

在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<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 建構函式深拷貝
  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.                 //操作符過載 + 使用成員函式
  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<<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. }


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

沒有問題

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2135175/,如需轉載,請註明出處,否則將追究法律責任。

相關文章