C++ 延续了 C 的函数模型,但增加了重载、默认参数、引用和移动相关规则。它们的共同目标是:让函数接口更准确地表达“需要什么”和“会做什么”。
函数重载
同一作用域中可以存在多个同名函数,只要参数列表能区分:
#include <iostream>
#include <string>
void print(int value) {
std::cout << "int: " << value << '\n';
}
void print(double value) {
std::cout << "double: " << value << '\n';
}
void print(const std::string& value) {
std::cout << "string: " << value << '\n';
}
2026/7/14大约 4 分钟