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

时间:2020-03-18 15:35:32   收藏:0   阅读:59
//函数重载的注意事项
//引用作为重载条件  加不加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;
}

 

原文:https://www.cnblogs.com/gjbhpu0308/p/12517382.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!