詳解C++編程中一元運算符的重載
可重載的一元運算符如下:
- !(邏輯“非”)
- &(取址)
- ~(二進制反碼)
- *(取消指針引用)
- +(一元加)
- -(一元求反)
- ++(遞增)
- --(遞減)
- 轉(zhuǎn)換運算符
后綴遞增和遞減運算符(++ 和 ––)在遞增和遞減中單獨處理,下面會講到。
以下規(guī)則適用于所有其他一元運算符。若要將一元運算符函數(shù)聲明為非靜態(tài)成員,則必須用以下形式聲明它:
ret-type operator op ()
其中 ret-type 是返回類型,op 是上表中列出的運算符之一。
若要將一元運算符函數(shù)聲明為全局函數(shù),則必須用以下形式聲明它:
ret-type operator op (arg )
其中 ret-type 和 op 如上所述用于成員運算符函數(shù),arg 是要參與運算的類類型的參數(shù)。
注意
一元運算符的返回類型沒有限制。例如,邏輯“非”(!) 返回整數(shù)值是合理的,但并非強制性的。
遞增和遞減運算符重載
由于遞增和遞減運算符各有兩個變量,因此它們屬于一個特殊類別:
- 前置遞增和后置遞增
- 前置遞減和后置遞減
編寫重載的運算符函數(shù)時,為這些運算符的前綴和后綴版本實現(xiàn)單獨的版本很有用。若要區(qū)分這兩者,請遵循以下規(guī)則:運算符的前綴形式與聲明任何其他一元運算符的方式完全相同;后綴形式接受 int 類型的其他參數(shù)。
注意
當為遞增或遞減運算符的前綴形式指定重載運算符時,其他參數(shù)的類型必須是 int;指定任何其他類型都將產(chǎn)生錯誤。
以下示例顯示如何為 Point 類定義前綴和后綴遞增和遞減運算符:
// increment_and_decrement1.cpp class Point { public: // Declare prefix and postfix increment operators. Point& operator++(); // Prefix increment operator. Point operator++(int); // Postfix increment operator. // Declare prefix and postfix decrement operators. Point& operator--(); // Prefix decrement operator. Point operator--(int); // Postfix decrement operator. // Define default constructor. Point() { _x = _y = 0; } // Define accessor functions. int x() { return _x; } int y() { return _y; } private: int _x, _y; }; // Define prefix increment operator. Point& Point::operator++() { _x++; _y++; return *this; } // Define postfix increment operator. Point Point::operator++(int) { Point temp = *this; ++*this; return temp; } // Define prefix decrement operator. Point& Point::operator--() { _x--; _y--; return *this; } // Define postfix decrement operator. Point Point::operator--(int) { Point temp = *this; --*this; return temp; } int main() { }
可使用以下函數(shù)頭在文件范圍中(全局)定義同一運算符:
friend Point& operator++( Point& ) // Prefix increment friend Point& operator++( Point&, int ) // Postfix increment friend Point& operator--( Point& ) // Prefix decrement friend Point& operator--( Point&, int ) // Postfix decrement
表示遞增或遞減運算符的后綴形式的 int 類型的參數(shù)不常用于傳遞參數(shù)。它通常包含值 0。但是,可按以下方式使用它:
// increment_and_decrement2.cpp class Int { public: Int &operator++( int n ); private: int _i; }; Int& Int::operator++( int n ) { if( n != 0 ) // Handle case where an argument is passed. _i += n; else _i++; // Handle case where no argument is passed. return *this; } int main() { Int i; i.operator++( 25 ); // Increment by 25. }
除顯式調(diào)用之外,沒有針對使用遞增或遞減運算符來傳遞這些值的語法,如前面的代碼所示。實現(xiàn)此功能的更直接的方法是重載加法/賦值運算符 (+=)。
相關(guān)文章
DataFrame:通過SparkSql將scala類轉(zhuǎn)為DataFrame的方法
今天小編就為大家分享一篇DataFrame:通過SparkSql將scala類轉(zhuǎn)為DataFrame的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Django對接elasticsearch實現(xiàn)全文檢索的示例代碼
搜索是很常用的功能,如果是千萬級的數(shù)據(jù)應該怎么檢索,本文主要介紹了Django對接elasticsearch實現(xiàn)全文檢索的示例代碼,感興趣的可以了解一下2021-08-08Python實現(xiàn)爬取天氣數(shù)據(jù)并可視化分析
這篇文章主要和大家分享一個用Python實現(xiàn)的小功能:獲取天氣數(shù)據(jù),進行可視化分析,帶你直觀了解天氣情況!感興趣的小伙伴可以學習一下2022-02-02python 動態(tài)調(diào)用函數(shù)實例解析
這篇文章主要介紹了python 動態(tài)調(diào)用函數(shù)實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10