C++前置++、後置++、不等號!及賦值運算符重載

  運算符重載的主要目的是爲了讓類對象能像普通數據類型一樣能夠進行加減乘除,自加自減等操作,非常直觀方便。現在來回顧C++的自加減(分前置與後置)以及不等號非運算符,賦值運算符的重載。

        1 ++重載

        (1)前置++運算符的重載方式:

        成員函數的重載: 函數類型& operator++()

        友元函數的重載:friend 函數類型& operator++(類類型& )


        (2)後置++運算符的重載方式:

        成員函數的重載:函數類型& operator++(int)

        友元函數的重載:friend 函數類型& operator++(類類型&, int)


        注意,爲了區分前置++與後置++的區別,需要在參數後增加一個"int"以示區分。含有"int"的重載方式爲後置++,否則爲前置++。前置--與後置--類似用法。前面說過,成員函數與友元函數的重載如果同時存在時,會先調用成員函數的重載,但是在++或--時,成員函數與友元函數的重載是不能同時存在的。

下面舉一個例子:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #ifndef _INTEGER_H_  
  2. #define _INTEGER_H_  
  3.   
  4. class Integer  
  5. {  
  6. public:  
  7.     Integer(int n);  
  8.     ~Integer();  
  9.     void Display();  
  10.   
  11.     Integer& operator++(); //成員方式重載前置++  
  12.     Integer& operator++(int); //成員方式重載後置++  
  13.   
  14.     friend Integer& operator++(Integer& i);//友元函數重載前置++  
  15.     friend Integer& operator++(Integer& i, int);//友元函數重載後置++  
  16.   
  17.       
  18. private:  
  19.     int n_;  
  20. };  
  21.   
  22. #endif  
  

        下面是它們的具體代碼實現:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #include "Integer.h"  
  2.   
  3. Integer::Integer(int n):n_(n){}  
  4.   
  5. Integer::~Integer(){}  
  6.   
  7. void Integer::Display() const  
  8. {  
  9.      cout << n_ << endl;  
  10. }  
  11.   
  12. //最好優先使用成員函數重載,  
  13.   
  14. //成員函數重載前置++  
  15. Integer& Integer::operator++()  
  16. {  
  17.     ++n_;  
  18.     return *this;  
  19. }  
  20.   
  21. //成員函數重載後置++  
  22. Integer& Integer::operator++(int)  
  23. {  
  24.     Integer tmp(n_);  
  25.     ++n_;  
  26.     return tmp;  
  27. }  
  28.   
  29. //友元重載前置++  
  30. Integer& operator++(Integer& i)  
  31. {  
  32.     ++i.n_;  
  33.     return i;  
  34. }  
  35.   
  36. //友元重載後置++  
  37. Integer& operator++(Integer& i, int)  
  38. {  
  39.     Integer tmp(i.n_);  
  40.     ++i.n_;  
  41.     return tmp;  
  42. }  

        關於!及=賦值運算符的重載以String類進行說明:

         下面是String類的定義:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #ifndef STRING_H_  
  2. #define STRING_H_  
  3.   
  4. class String  
  5. {  
  6. public:  
  7.     explicit String(const char *str="");  
  8.     String(const String& other);  
  9.     ~String();  
  10.   
  11.     //應用於s="abc";的情況  
  12.     String& operator=(const char *str);//重載=  
  13.   
  14.     bool operator!() const//重載!  
  15.   
  16.     void Display() const;  
  17.   
  18. private:  
  19.     char* str_;  
  20.     char* AllocAndCopy(const char* str);   
  21. };  
  22.   
  23. #endif  

        下面是具體實現:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #include <string.h>  
  2. #include <iostream>  
  3. #include "String.h"  
  4. using namespace std;  
  5.   
  6. String::String(const char *str)  
  7. {  
  8.     str_ = AllocAndCopy(str);  
  9. }  
  10.   
  11. String::~String()  
  12. {  
  13.     delete [] str_;  
  14. }  
  15.   
  16. char* String::AllocAndCopy(const char* str)  
  17. {  
  18.     int len = strlen(str)+1;  
  19.     char* newstr = new char[len];  
  20.     memset(newstr, 0, len);  
  21.     strcpy(newstr, str);  
  22.   
  23.     return newstr;  
  24. }  
  25.   
  26.   
  27. //深拷貝,copy assign  
  28. String& String::operator=(const String& other)   //s1 = s2  
  29. {  
  30.     ifthis == &other)  
  31.         return *this;  
  32.       
  33.     delete [] str_;  
  34.     str_ = AllocAndCopy(other.str_);  
  35.     return *this;  
  36. }  
  37.   
  38. String& String::operator=(const char* str)  //s1="abc"  
  39. {  
  40.     delete []  str_;  
  41.     str_ = AllocAndCopy(str);  
  42.     return *this;  
  43. }  
  44.   
  45. bool String::operator!() const  
  46. {  
  47.     return (strlen(str_) != 0 )  
  48. }  
  49.   
  50. void String::Display() const  
  51. {  
  52.     cout << str_ << endl;  
  53. }  

        針對String類寫的一個簡單的用例:

        

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #include "String.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     String s1("abc");  
  8.     String s2(s1);//copy.  
  9.     String s3;//帶默認參數  
  10.       
  11.     s3 = s1; //調用賦值運算符  
  12.     s3.Display();  
  13.   
  14.   
  15. //默認的處理方式爲:將字符串構造成String類對象,再賦值至s3,如果在構造函數前加上explicit聲明,則會發生編譯錯誤。解決的方式需要重載一個參數爲const char *的等號運算符即可  
  16.     s3 = "xxx"//調用參數爲const char *的賦值運算符  
  17.       
  18.     String s4;  
  19.     bool notempty;  
  20.     notempty = !s4;   
  21.     cout << notempty << endl; //1  
  22.   
  23.     s4 = "aaa";  
  24.     notempty = !s4;  
  25.     cout << notempty << endl; //0  
  26.   
  27.     return 0;  
  28.       
  29. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章