函数重载遇到引用和默认参数

Posted on 2020-03-18 14:50  小葛笔记  阅读(408)  评论(0)    收藏  举报
//函数重载的注意事项
//引用作为重载条件  加不加const可以作为函数重载条件 
//默认参数遇到重载条件,此种情况要避免 
#include<iostream>
using namespace std;
void func(int &r)
{
    cout << "func(int &r)函数调用" <<endl; 
}
void func(const int &r)
{
    cout << "func(const int &r)函数调用" <<endl; 
}
void fun(int a)
{
    cout<< "fun(int a)" << endl;
} 
void fun(int a,int b = 20)
{
    cout<< "fun(int a,int b )" << endl;
} 
int main() 
{
    int a = 10;
    //func(a);  //对第一个函数的调用
    const int v = 10;
    func(v); // 对第二个函数的调用 
    func(10); //对第二个函数的调用
    int b = 10; 
    fun(a,b);  //调用错误 
    system("pause");  
    return 0;
}

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3