C++重载函数

定义

如果在同一作用域内的几个函数名字相同但是形参列表不同,我们称之为重载函数
从重载函数的定义上来看,它们应该在形参数量或者形参类型上有所不同,和返回值的类型没有任何关系
注:main 函数不能重载

重载和const 形参
形参为值变量
int fun(int a){}
int fun(const int a){}  //重复定义fun

定义为const的变量,只是把当做左值进行修改时,才会报错。因此,加上const 关键字是否构成重载就看在函数的定义中形参的意义是否相同。
在下面fun()函数中,假如有一个实参i,向形参传值的过程中,可以将一个普通变量赋值给常量,和一个普通变量赋值给第一个函数中的形参无任何区别。故上面两个函数不构成重载。
在C++编译器中,我们做实验验证下:

#include<iostream>
#include <typeinfo>
using namespace std;
int fun(int a){ return 0; }
int fun(const int a){ return 0; }
int main()
{
    int c;
    const int d=0;

    cout << typeid(c).name() << endl;   //参数为int型
    cout << typeid(d).name() << endl;   //参数为int型
    return 0;
}
形参为指针变量
情况1
int fun(int *a){ return 0; }
int fun(const int *a){ return 0; }

编写如下的代码:

#include<iostream>
#include <typeinfo>
using namespace std;
int fun(int *a){
    cout << "fun(int *a)" << endl;
    return 0;
}
int fun(const int *a){
    cout << "fun(const int *a)" << endl;
    return 0; 
}
int main()
{
    int *a=nullptr;
    const int *b = nullptr;

    cout << typeid(a).name() << endl;   //int *
    cout << typeid(b).name() << endl;   //int const *

    fun(a);    //自动寻找最佳匹配,fun(int *a)
    fun(b);   //自动寻找最接匹配函数,fun(const int *a)  一般来说,指针类型需要严格匹配
    return 0;
}

在函数匹配过程中,会根据实参进行最佳类型匹配,故而上面两个函数可以进行重载。

情况2
int fun(int *a){ return 0; }
int fun(int const *a){ return 0; }

在情况1中,第二个函数的定义和情况2中第二个函数的定义相同,无任何区别。所以上面两个函数也是可以重载的。
编写如下的代码进行验证:

#include<iostream>
#include <typeinfo>
using namespace std;
int fun(int *a){
    cout << "fun(int *a)" << endl;
    return 0;
}
int fun(int const  *a){
    cout << "fun(int const  *a)" << endl;
    return 0; 
}
int main()
{
    int *a=nullptr;
    const int *b = nullptr;
    fun(a);    //自动寻找最佳匹配,fun(int *a)
    fun(b);   //自动寻找最接匹配函数,fun(const int *a)  一般来说,指针类型需要严格匹配

    cout << typeid(a).name() << endl;   //int *
    cout << typeid(b).name() << endl;   //int const *
    return 0;
}

结论同情况1

情况3
int fun(int *a){ return 0; }
int fun(int *const a){ return 0; }   //错误,重复定义fun

测试代码是这样:

#include<iostream>
#include <typeinfo>
using namespace std;

int main()
{
    int *a=nullptr;
    int *const b = nullptr;

    cout << typeid(a).name() << endl;   //int *
    cout << typeid(b).name() << endl;   //int *
    return 0;
}

即上面两个函数的定义相同,不构成重载。

形参为引用变量
情况1
void func(int &a)
void func(const int &a)

测试代码:

#include<iostream>
#include <typeinfo>
using namespace std;

void func(int &a)
{
    cout << "fun(int &a)" << endl;
} 
void func(const int &a)
{
    cout << "fun(const int &a)" << endl;
} 

int main()
{
    int i = 10;
    int &a=i;
    const int &b = i;
    func(a);//调用第一个func(int &a)函数
    func(b);//调用第一个func(const int &a)函数,最佳匹配第二个函数

    cout << typeid(a).name() << endl;   //int 
    cout << typeid(b).name() << endl;   //int 
    return 0;
}

结论:虽然在主函数中引用的数据类型都是int,但是上述两个函数可以构成重载。主要是因为当你传常量引用时,不能用一个非常量引用来引用一个常量,所以上述fun()函数可以相互匹配。

情况2
void func(int &a)
void func(int  const &b)

测试代码:

#include<iostream>
#include <typeinfo>
using namespace std;

void func(int &a)
{
    cout << "fun(int &a)" << endl;
} 
void func(int  const &b)
{
    cout << "fun(int  const &b)" << endl;
} 

int main()
{
    int i = 10;
    int &a=i;
    int  const &b = i;
    func(a);
    func(b);

    cout << typeid(a).name() << endl;   //int 
    cout << typeid(b).name() << endl;   //int 
    return 0;
}

结论同情况1

情况3
void func(int &a)
void func(int &const b)

测试代码:

#include<iostream>
#include <typeinfo>
using namespace std;

void func(int &a)
{
    cout << "fun(int &a)" << endl;
} 
void func(int &const b)
{
    cout << "fun(int &const b)" << endl;
} 

int main()
{
    int i = 10;
    int &a=i;
    int & const b = i;
    func(a);  //均调用第一个fun(int &a)函数
    func(b);  //均调用第一个fun(int &a)函数

    cout << typeid(a).name() << endl;   //int 
    cout << typeid(b).name() << endl;   //int 
    return 0;
}

结论:上述两个函数不构成重载。

总结

在上面的分析中,我们可以看出:
(1)C++的形参可以传值,传指针,传引用,同时可以给对应的形参加上const。
(2)在普通形参和const 形参是否构成重载,大致来看分为两种情况:

  1. 如果是对于传值,那么普通传值和const传值不构成重载。编译器对两个的类型解析是相同的。(为什么会这样解析?应该能看源码吧,还没到这个阶段)
  2. 如果是对于指针传值,那么存在区别。对于
const int *p=nullptr;
int const *p=nullptr;

编译器均解析为 int const *;
对于

int * const b=nullptr;

编译器解析为int *
所以对于指针传参,从类型上就可以区分出来是否构称重载。
3. 如果对于引用,因为引用本身是别名,并不是一个对象。因此,在上述测试代码中,求引用的类型都是对引用的变量求类型。但是在传参中是否构成重载函数,其实和指针的情况完全相同。
总结完毕,欢迎指正!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值