重载让同一名字根据参数类型等规则对应不同函数;编译器需要选出合适的候选。下面区分函数、运算符、构造函数和转换函数,并纠正原表把基类/派生类析构称为“C++11 析构函数重载”的说法。
四类常见用法与一个误区
跳转到“四类常见用法与一个误区”| 原表主题 | 正确理解 | 示例 |
|---|---|---|
| 函数重载 | 同名函数可具有不同参数列表;仅返回类型不同不能构成普通重载 | print(int)、print(double)、print(const std::string&) |
| 运算符重载 | 给已有运算符定义适用于用户类型的函数 | Point::operator+;不能改变优先级或参数个数 |
| 构造函数重载 | 用不同参数形式构造同一种类 | Rectangle()、Rectangle(int, int) |
| 转换函数 | 用 operator T() 定义向类型 T 的转换,可加 explicit 限制隐式转换 | explicit operator std::string() const |
| 基类/派生类析构 | ~Base()、~Derived() 属于不同类,并不是同一类里的普通重载 | 基类虚析构使通过基类指针删除派生对象时调用正确的析构链 |
重载决议与 virtual 动态分派是不同机制;同名不代表一定能找到无歧义调用。函数不能仅靠返回类型区分;按值参数的顶层 const 也不会额外形成不同的参数类型。可重载声明、转换函数。
C++17 完整示例
跳转到“C++17 完整示例”以下代码逐项演示原表想表达的用途。坐标和尺寸使用固定的小整数;实际处理外部数据时还须按业务范围检查数值。
#include <cassert>#include <iostream>#include <memory>#include <sstream>#include <string>#include <type_traits>
std::string print(int value) { return "int:" + std::to_string(value); }std::string print(double value) { std::ostringstream text; text << "double:" << value; return text.str();}std::string print(const std::string& value) { return "string:" + value; }
struct Point { int x; int y; Point operator+(const Point& other) const { return {x + other.x, y + other.y}; }};
class Rectangle { int width_; int height_;public: Rectangle() : Rectangle(1, 1) {} Rectangle(int width, int height) : width_(width), height_(height) {} int area() const { return width_ * height_; } explicit operator std::string() const { return std::to_string(width_) + "x" + std::to_string(height_); }};
struct Base { int& baseDestroyed; explicit Base(int& count) : baseDestroyed(count) {} virtual ~Base() { ++baseDestroyed; }};struct Derived : Base { int& derivedDestroyed; Derived(int& baseCount, int& derivedCount) : Base(baseCount), derivedDestroyed(derivedCount) {} ~Derived() override { ++derivedDestroyed; }};
int main() { assert(print(7) == "int:7"); assert(print(2.5) == "double:2.5"); assert(print(std::string("hello")) == "string:hello"); const Point sum = Point{1, 2} + Point{3, 4}; assert(sum.x == 4 && sum.y == 6); const Rectangle unit; const Rectangle rectangle(3, 4); assert(unit.area() == 1 && rectangle.area() == 12); static_assert(!std::is_convertible_v<Rectangle, std::string>); assert(static_cast<std::string>(rectangle) == "3x4"); int baseDestroyed = 0; int derivedDestroyed = 0; { std::unique_ptr<Base> object = std::make_unique<Derived>(baseDestroyed, derivedDestroyed); } assert(baseDestroyed == 1 && derivedDestroyed == 1); std::cout << "overloads, explicit conversion and virtual destruction: OK\n";}explicit 转换函数仍可通过 static_cast<std::string> 显式调用。参数类型、隐式转换链和默认参数都可能影响是否存在最佳候选;设计接口时应先保证调用含义清楚,而不是尽量复用同一个名字。
析构函数的 C++20 特殊规则
跳转到“析构函数的 C++20 特殊规则”普通非模板类不能通过给析构函数增加参数或重复定义同一个析构函数来重载它。C++20 在类模板中允许多个带约束的候选析构函数,再为特定实例选择析构函数;这是专门规则,不能用原来的 ~Base()/~Derived() 两行来说明。C++20 析构规则。
// constrained-destructors.cpp — C++20#include <cassert>#include <iostream>#include <type_traits>
template<class T>struct Tracked { int& selected; ~Tracked() requires std::is_integral_v<T> { selected = 1; } ~Tracked() requires (!std::is_integral_v<T>) { selected = 2; }};
int main() { int integral = 0; int other = 0; { Tracked<int> object{integral}; } { Tracked<double> object{other}; } assert(integral == 1 && other == 2); std::cout << "constrained destructors: selected 1 and 2\n";}该小例子需要 C++20 模式。运算符的成员/非成员形式、优先级限制和转换边界另见运算符与类型转换。