C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载。
重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义(实现)不相同。
当您调用一个重载函数或重载运算符时,编译器通过把您所使用的参数类型与定义中的参数类型进行比较,决定选用最合适的定义。选择最合适的重载函数或重载运算符的过程,称为重载决策。
运算符重载
您可以重定义或重载大部分 C++ 内置的运算符。这样,您就能使用自定义类型的运算符。
重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。
Box operator+(const Box&);
声明加法运算符用于把两个 Box 对象相加,返回最终的 Box 对象。大多数的重载运算符可被定义为普通的非成员函数或者被定义为类成员函数。如果我们定义上面的函数为类的非成员函数,那么我们需要为每次操作传递两个参数,如下所示:
Box operator+(const Box&, const Box&);
可以重载的运算符有
运算符 | 运算符个数 |
---|---|
双目算术运算符 | + (加),-(减),*(乘),/(除),% (取模) |
关系运算符 | ==(等于),!= (不等于),< (小于),> (大于),<=(小于等于),>=(大于等于) |
逻辑运算符 | ||(逻辑或),&&(逻辑与),!(逻辑非) |
单目运算符 | + (正),-(负),*(指针),&(取地址) |
自增自减运算符 | ++(自增),–(自减) |
位运算符 | | (按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移) |
赋值运算符 | =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>= |
空间申请与释放 | new, delete, new[ ] , delete[] |
其他运算符 | ()(函数调用),->(成员访问),,(逗号),[](下标) |
我们可以重载一下加号
#include <iostream>
#include <string>
class Box{
private:
int length;
public:
Box();
Box(int length_);
Box operator+(const Box &b);
int getlength();
};
Box::Box() {
length = 1;
}
Box::Box(int length_) {
length = length_;
}
int Box::getlength() {
return length;
}
Box Box::operator+(const Box &b) {
Box c = Box(0);
c.length = this->length + b.length;
return c;
}
int main() {
Box b1 = {1};
Box b2 = {2};
Box c = b1 + b2;
std::cout << c.getlength() << std::endl;
return 0;
}
我们也可以有另外一种写法,不作为类的成员函数定义
#include <iostream>
#include <string>
class Box {
private:
int length;
public:
Box();
Box(int length_);
int getlength();
void setlength(int length_);
};
Box::Box() {
length = 1;
}
Box::Box(int length_) {
length = length_;
}
int Box::getlength() {
return length;
}
void Box::setlength(int length_) {
length = length_;
}
Box operator+(Box &m, Box &n) {
Box c = Box(0);
c.setlength(m.getlength() + n.getlength());
return c;
}
int main() {
Box b1 = {1};
Box b2 = {2};
Box c = b1 + b2;
std::cout << c.getlength() << std::endl;
return 0;
}
这样写比较麻烦,主要是这样写的话重载运算符函数不是类的成员函数,不能直接访问类的私有属性,所以我们需要写类的属性的接口。
我们也可以用友元重载
#include <iostream>
#include <string>
class Box {
private:
int length;
public:
Box();
Box(int length_);
int getlength() {return length;};
friend Box operator+(const Box &a, const Box &b);
};
Box::Box() {
length = 1;
}
Box::Box(int length_) {
length = length_;
}
Box operator+(const Box &a, const Box &b) {
Box c = Box(0);
c.length = a.length + b.length;
return c;
}
int main() {
Box b1 = {1};
Box b2 = {2};
Box c = b1 + b2;
std::cout << c.getlength() << std::endl;
return 0;
}
友元可以直接访问对象的数据,所以会很方便。
我们在进行重载操作时要避开全局的重载,否则在进行其他操作时会被干扰到。
重载可以减少编程时间和代码量,但是会让编程的难度加大,出错的概率变高。
即使我们重载了运算符,也不会改变运算符的优先级,就上面我们重载的是加号,但是这个加号的优先级依然低于乘号。