123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- //函数重载的注意事项
- //⚪引用作为重载条件
- //⚪函数重载碰到函数作为默认参数
- #include <iostream>
- //1、引用作为重载条件
- void func(int &a) //int &a = 10; 不合法
- {
- std::cout << "func(int &a)的 调用" << std::endl;
- }
- void func(const int& a) //const int &a = 10; 加const后编译器帮我们优化 加入临时变量 合法
- {
- std::cout << "func(const int &a)的 调用" << std::endl;
- }
- //2、函数重载碰到默认参数
- void func2(int a, int b = 10)
- {
- std::cout << "func2(int a, int b) 的调用" << std::endl;
- }
- void func2(int a)
- {
- std::cout << "func2(int a) 的调用" << std::endl;
- }
- int main()
- {
- //int a = 10;
- //func(a); //调用的是没有const的函数,a 是一个变量(可读可写),加const限制为只读状态
- //func(10);
- //func2(10); //当函数重载碰到默认参数,出现二义性,报错,尽量避免这种情况
- func2(10, 20);
- system("pause");
- return 0;
- }
|