工具標籤
C++ 重載運算子和重載函數
C++ 允許在同一作用域中的某個函數和運算子指定多個定義,分別稱為函數重載和運算子重載。
重載聲明是指一個與先前已經在該作用域內宣告過的函數或方法具有相同名稱的聲明,但是它們的參數清單和定義(實作)不相同。
當您呼叫一個重載函數或重載運算子時,編譯器透過將您所使用的參數類型與定義中的參數類型進行比較,決定選用最適合的定義。選擇最合適的重載函數或重載運算子的過程,稱為重載決策。
C++ 中的函數重載
在同一個作用域內,可以宣告幾個功能類似的同名函數,但是這些同名函數的形式參數(指參數的數量、型別或順序)必須不同。您不能僅透過傳回類型的不同來重載函數。
在下面的實例中,同名函數print() 被用來輸出不同的資料類型:
#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;
}當上面的程式碼被編譯和執行時,它會產生下列結果:
Printing int: 5 Printing float: 500.263 Printing character: Hello C++
C++ 中的運算子重載
您可以重定義或重載大部分C++ 內建的運算子。這樣,您就能使用自訂類型的運算子。
重載的運算子是帶有特殊名稱的函數,函數名稱是由關鍵字 operator 和其後要重載的運算子符號構成的。與其他函數一樣,重載運算子有一個返回類型和一個參數列表。
Box operator+(const Box&);
宣告加法運算子用來把兩個 Box 物件相加,傳回最終的 Box 物件。大多數的重載運算子可被定義為普通的非成員函數或定義為類別成員函數。如果我們定義上面的函數為類別的非成員函數,那麼我們需要為每次運算傳遞兩個參數,如下所示:
Box operator+(const Box&, const Box&);
下面的實例使用成員函數示範了運算子重載的概念。在這裡,物件作為參數進行傳遞,物件的屬性使用this 運算子進行訪問,如下所示:
#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;
}
// 重载 + 运算符,用于把两个 Box 对象相加
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; // 长度
double breadth; // 宽度
double height; // 高度
};
// 程序的主函数
int main( )
{
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
Box Box3; // 声明 Box3,类型为 Box
double volume = 0.0; // 把体积存储在该变量中
// 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;
// 把两个对象相加,得到 Box3
Box3 = Box1 + Box2;
// Box3 的体积
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}當上面的程式碼被編譯和執行時,它會產生下列結果:
Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400
可重載運算子/不可重載運算子
下面是可重載的運算子清單:
| + | - | * | / | % | ^ |
| & | | | ~ | ! | , | #= |
| < | > | <= | #>= | ++ | -- |
| #<< | |||||
| == | #!= | #& | || | += | |
| /= | %= | ^= | # &= | |= | |
| <<= | >>= | [] | () | -> |
new []
| #下面是不可重載的運算子清單: |
運算子重載實例
下面提供了各種運算子重載的實例,幫助您更能理解重載的概念。
| 序號 | #運算子與實例 |
|---|---|
| #1 | 一元運算子重載 |
| 2 | 二元運算子重載 |
| #3 | 關係運算子重載 |
| 4 | 輸入/輸出運算子重載 |
| #5 | ++ 和-- 運算子重載 |
| 6 | 賦值運算子重載 |
| #7 | 函數呼叫運算子() 重載 |
| 8 | 下標運算子[] 重載 |
| #9 | 類別成員存取運算子-> 重載 |
相關影片
熱AI工具
免費脫衣圖片
用於從照片中去除衣服的線上人工智慧工具。
人工智慧驅動的應用程序,用於創建逼真的裸體照片
人工智慧支援投資研究,做出更明智的決策
熱門文章
熱門話題
20450
7
13593
4
11933
4
8937
17
8479
7
熱門工具
好用且免費的程式碼編輯器
中文版,非常好用
強大的PHP整合開發環境
視覺化網頁開發工具
神級程式碼編輯軟體(SublimeText3)












![PHP實戰開發極速入門: PHP快速創建[小型商業論壇]](https://img.php.cn/upload/course/000/000/035/5d27fb58823dc974.jpg)
