C++ 重载(运算符和函数)
C++ 允许你在同一作用域内为一个 function 名称或一个 operator 指定多个定义,这分别称为 function overloading 和 operator overloading。
重载声明是指在同一作用域内与先前声明具有相同名称的声明,但两者具有不同的参数且显然不同的定义(实现)。
当你调用一个重载的 function 或 operator 时,编译器会通过比较你用于调用 function 或 operator 的实参类型与定义中指定的形参类型,来确定使用最合适的定义。这个选择最合适的重载 function 或 operator 的过程称为 overload resolution。
C++ 中的 Function Overloading
你可以在同一作用域内为同一个 function 名称提供多个定义。这些 function 的定义必须在参数列表的类型和/或参数数量上有所不同。你不能仅通过返回类型不同来重载 function 声明。
阅读: C++ Function Overloading
以下示例中,同一个 print() function 被用于打印不同数据类型 —
#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;
// 调用 print 打印整数
pd.print(5);
// 调用 print 打印浮点数
pd.print(500.263);
// 调用 print 打印字符
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; // 在此处存储盒子的体积
// box 1 规格
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 规格
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// box 1 的体积
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// box 2 的体积
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// 如下相加两个对象:
Box3 = Box1 + Box2;
// box 3 的体积
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
上述代码编译并执行后,将产生以下结果 −
Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400
可重载/不可重载运算符
以下是可以重载的运算符列表 −
| + | - | * | / | % | ^ |
| & | | | ~ | ! | , | = |
| < | > | <= | >= | ++ | -- |
| << | >> | == | != | && | || |
| += | -= | /= | %= | ^= | &= |
| |= | *= | <<= | >>= | [] | () |
| -> | ->* | new | new [] | delete | delete [] |
以下是不能重载的运算符列表 −
| :: | .* | . | ?: |
运算符重载示例
以下是各种运算符重载示例,帮助您理解该概念。
| 序号 | 运算符 & 示例 |
|---|---|
| 1 | 一元运算符重载 |
| 2 | 二元运算符重载 |
| 3 | 关系运算符重载 |
| 4 | 输入/输出运算符重载 |
| 5 | ++ 和 -- 运算符重载 |
| 6 | 赋值运算符重载 |
| 7 | 函数调用 () 运算符重载 |
| 8 | 下标 [] 运算符重载 |
| 9 | 类成员访问运算符 -> 重载 |