自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++操作符重載不同方式區(qū)別

開發(fā) 后端
大家在使用C++操作符重載的時(shí)候,往往會(huì)遇到一些麻煩,就是午飯判斷++(--)是在操作數(shù)前面還是在操作數(shù)后面。我們將會(huì)在這里詳細(xì)介紹正確的應(yīng)用方法。

C++編程語言可以被看做是C語言的升級(jí)版本,它能夠支持C語言中的所有功能,而且在其他方面也有很大的提升。其中,在C++操作符重載中++,--需要說明是++(--)在操作數(shù)前面,還是在操作數(shù)后面,區(qū)別如下:

C++操作符重載代碼經(jīng)過測試無誤(起碼我這里沒問題^_^)

  1. #include < iostream> 
  2. #include < cstdlib> 
  3. using namespace std;  
  4. template< typename T> class A  
  5. {  
  6. public:  
  7. A(): m_(0){  
  8. }  
  9. // +  
  10. const T operator + (const T& rhs)  
  11. {  
  12. // need to be repaired , but see it is only a demo  
  13. return (this->m_ + rhs);  
  14. }  
  15. // -  
  16. const T operator - (const T& rhs){  
  17. // need to be repaired , but see it is only a demo  
  18. return (this->m_ - rhs);  
  19. }  
  20. T getM(){  
  21. return m_;  
  22. }  
  23. // ++在前的模式,這里返回的是引用 ,準(zhǔn)許++++A  
  24. A& operator ++ (){  
  25. (this->m_)++;  
  26. return *this;  
  27. }  
  28. // ++ 在后,這里返回的是一個(gè)新的A類型變量,且不可改變  
  29. // 目的是防止出現(xiàn) A++++情況  
  30. const A operator ++(int a){  
  31. A< T> b = *this;  
  32. (this->m_)++;  
  33. return b;  
  34. }  
  35. private:  
  36. T m_;  
  37. };  
  38. int main(void){  
  39. int i = 0;  
  40. cout< < ++++i< < endl;  
  41. // i++++ is not allowed  
  42. A< int> a;  
  43. A< int> b = ++a;  
  44. cout< < b.getM()< < endl;  
  45. A< int> c = a++;  
  46. cout< < c.getM()< < endl;  
  47. cout< < a.getM()< < endl;  
  48. int t = a+2;  
  49. cout< < t< < endl;  
  50. system("pause");  
  51. return 0;   

以上就是對C++操作符重載的相關(guān)介紹。

【編輯推薦】

  1. C++基本函數(shù)代碼示例
  2. C++斷點(diǎn)無效解決方案
  3. C++名字空間基本內(nèi)容講述
  4. C++中英文字符串基本概念解析
  5. C++連接SQL數(shù)據(jù)庫分步驟進(jìn)行
責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-02-03 10:23:47

C++操作符重載

2009-08-18 18:06:54

C#操作符重載

2010-02-04 15:41:10

C++內(nèi)存管理

2010-01-28 11:16:28

C++操作符

2010-01-27 11:00:17

C++操作符

2009-08-18 17:42:12

C#操作符重載

2009-08-18 17:55:20

C#操作符重載

2010-01-19 13:32:20

C++操作符

2010-01-21 09:53:23

C++操作符

2009-07-14 18:34:22

Jython操作符重載

2010-02-04 10:33:40

C++異常傳遞

2009-08-18 17:20:17

C#操作符重載

2009-08-18 17:34:25

C#操作符重載應(yīng)用

2009-08-19 17:26:28

C# 操作符

2023-10-12 09:58:45

操作符C++

2010-02-04 10:13:35

C++獲得系統(tǒng)時(shí)間

2020-06-17 12:22:44

C覆蓋重載

2009-08-19 17:20:22

C# 操作符

2015-07-08 16:07:19

iOSObjective-C

2010-02-04 09:26:23

C++模板函數(shù)重載
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)