C++ 在 C 的基础上增加了引用、函数重载、命名空间等语法,让代码更安全、更易组织。
本节要点
- 引用是变量的别名,必须在定义时初始化。
- 同一作用域内可以重载参数列表不同的函数。
- auto 可以让编译器自动推断类型。
代码示例
#include <iostream>
#include <string>
void print(int value) {
std::cout << "整数:" << value << std::endl;
}
void print(const std::string &value) {
std::cout << "字符串:" << value << std::endl;
}
int main() {
auto name = std::string("天下书楼");
print(42);
print(name);
return 0;
}