C++ 重載運(yùn)算符和重載函數(shù)
C++ 允許在同一作用域中的某個(gè)函數(shù)和運(yùn)算符指定多個(gè)定義,分別稱為函數(shù)重載和運(yùn)算符重載。
重載聲明是指一個(gè)與之前已經(jīng)在該作用域內(nèi)聲明過的函數(shù)或方法具有相同名稱的聲明,但是它們的參數(shù)列表和定義(實(shí)現(xiàn))不相同。
當(dāng)您調(diào)用一個(gè)重載函數(shù)或重載運(yùn)算符時(shí),編譯器通過把您所使用的參數(shù)類型與定義中的參數(shù)類型進(jìn)行比較,決定選用最合適的定義。選擇最合適的重載函數(shù)或重載運(yùn)算符的過程,稱為重載決策。
C++ 中的函數(shù)重載
在同一個(gè)作用域內(nèi),可以聲明幾個(gè)功能類似的同名函數(shù),但是這些同名函數(shù)的形式參數(shù)(指參數(shù)的個(gè)數(shù)、類型或者順序)必須不同。您不能僅通過返回類型的不同來(lái)重載函數(shù)。
下面的實(shí)例中,同名函數(shù) print() 被用于輸出不同的數(shù)據(jù)類型:
#include <iostream> using namespace std; class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; // Call print to print integer pd.print(5); // Call print to print float pd.print(500.263); // Call print to print character pd.print("Hello C++"); return 0; }
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
Printing int: 5 Printing float: 500.263 Printing character: Hello C++
C++ 中的運(yùn)算符重載
您可以重定義或重載大部分 C++ 內(nèi)置的運(yùn)算符。這樣,您就能使用自定義類型的運(yùn)算符。
重載的運(yùn)算符是帶有特殊名稱的函數(shù),函數(shù)名是由關(guān)鍵字 operator 和其后要重載的運(yùn)算符符號(hào)構(gòu)成的。與其他函數(shù)一樣,重載運(yùn)算符有一個(gè)返回類型和一個(gè)參數(shù)列表。
Box operator+(const Box&);
聲明加法運(yùn)算符用于把兩個(gè) Box 對(duì)象相加,返回最終的 Box 對(duì)象。大多數(shù)的重載運(yùn)算符可被定義為普通的非成員函數(shù)或者被定義為類成員函數(shù)。如果我們定義上面的函數(shù)為類的非成員函數(shù),那么我們需要為每次操作傳遞兩個(gè)參數(shù),如下所示:
Box operator+(const Box&, const Box&);
下面的實(shí)例使用成員函數(shù)演示了運(yùn)算符重載的概念。在這里,對(duì)象作為參數(shù)進(jìn)行傳遞,對(duì)象的屬性使用 this 運(yùn)算符進(jìn)行訪問,如下所示:
#include <iostream> using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; } void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } // 重載 + 運(yùn)算符,用于把兩個(gè) Box 對(duì)象相加 Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } private: double length; // 長(zhǎng)度 double breadth; // 寬度 double height; // 高度 }; // 程序的主函數(shù) int main( ) { Box Box1; // 聲明 Box1,類型為 Box Box Box2; // 聲明 Box2,類型為 Box Box Box3; // 聲明 Box3,類型為 Box double volume = 0.0; // 把體積存儲(chǔ)在該變量中 // Box1 詳述 Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // Box2 詳述 Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); // Box1 的體積 volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; // Box2 的體積 volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; // 把兩個(gè)對(duì)象相加,得到 Box3 Box3 = Box1 + Box2; // Box3 的體積 volume = Box3.getVolume(); cout << "Volume of Box3 : " << volume <<endl; return 0; }
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400
可重載運(yùn)算符/不可重載運(yùn)算符
下面是可重載的運(yùn)算符列表:
+ | - | * | / | % | ^ |
& | | | ~ | ! | , | = |
< | > | <= | >= | ++ | -- |
<< | >> | == | != | && | || |
+= | -= | /= | %= | ^= | &= |
|= | *= | <<= | >>= | [] | () |
-> | ->* | new | new [] | delete | delete [] |
下面是不可重載的運(yùn)算符列表:
:: | .* | . | ?: |
運(yùn)算符重載實(shí)例
下面提供了各種運(yùn)算符重載的實(shí)例,幫助您更好地理解重載的概念。
序號(hào) | 運(yùn)算符和實(shí)例 |
---|---|
1 | 一元運(yùn)算符重載 |
2 | 二元運(yùn)算符重載 |
3 | 關(guān)系運(yùn)算符重載 |
4 | 輸入/輸出運(yùn)算符重載 |
5 | ++ 和 -- 運(yùn)算符重載 |
6 | 賦值運(yùn)算符重載 |
7 | 函數(shù)調(diào)用運(yùn)算符 () 重載 |
8 | 下標(biāo)運(yùn)算符 [] 重載 |
9 | 類成員訪問運(yùn)算符 -> 重載 |