C++重载优先级

#include  < iostream >
using namespace std;
void funa (char* p) {
    cout << 1 << endl;
}
void funa (char const* p) {
    cout << 2 << endl;
}
void funb (char const* p, char c) {
    cout << 1 << endl;
}
void funb (char*p, int n) {
    cout << 2 << endl;
}
void func (char c) {
    cout << 1 << endl;
}
void func (int n) {
    cout << 2 << endl;
}
void func (long l) {
    cout << 3 << endl;
}
void fund (int n, void* p) {
    cout << 1 << endl;
}
void fund (double d, ...) {
    cout << 2 << endl;
}
int main (void) {
    char* p;
    funa (p); // 完全匹配优于常量转换
    char c;
    funb (p, c); // 常量转换优于升级转换
    short h;
    func (h); // 升级转换优于标准转换
    // 但也没有必要过分升级
    double d;
    void* pv;
    fund (d, pv); // 可变长参数表匹配度最低
    return 0;
}

如上
1. 完全匹配优于常量转换
2. 常量转换优于升级转换
3. 升级转换优于标准转换,但也没有必要过分升级
4. 可变长参数表匹配度最低

但基于不同的编译器,会编译结果有所不同,gcc报警告,但是funb和fund可以编译过,mscv和mingw下编译直接报错。


MSVC:
test.cpp(34): error C2666: “funb”: 2 个重载有相似的转换
1> test.cpp(12): 可能是“void funb(char *,int)”
1> test.cpp(9): 或 “void funb(const char *,char)”
1> 尝试匹配参数列表“(char *, char)”时
1> 注意: 限定调整(const/volatile)可能会造成多义性
1>test.cpp(40): error C2666: “fund”: 2 个重载有相似的转换
1> test.cpp(27): 可能是“void fund(double,…)”
1> test.cpp(24): 或 “void fund(int,void *)”
1> 尝试匹配参数列表“(double, void *)”时


Mingw:
——————–配置: mingw5 - CUI Debug, 编译器类型: MinGW——————–

检查文件依赖性…
正在编译 D:\Program FilesGreen\360DesktopLite\360DesktopLite 64位\skin\未命名1.cpp…
[Error] D:\Program FilesGreen\360DesktopLite\360DesktopLite 64位\skin\未命名1.cpp:34: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
[Warning] D:\Program FilesGreen\360DesktopLite\360DesktopLite 64位\skin\未命名1.cpp:9: note: candidate 1: void funb(const char*, char)
[Warning] D:\Program FilesGreen\360DesktopLite\360DesktopLite 64位\skin\未命名1.cpp:12: note: candidate 2: void funb(char*, int)
[Error] D:\Program FilesGreen\360DesktopLite\360DesktopLite 64位\skin\未命名1.cpp:40: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
[Warning] D:\Program FilesGreen\360DesktopLite\360DesktopLite 64位\skin\未命名1.cpp:24: note: candidate 1: void fund(int, void*)
[Warning] D:\Program FilesGreen\360DesktopLite\360DesktopLite 64位\skin\未命名1.cpp:27: note: candidate 2: void fund(double, …)
[Warning] D:\Program FilesGreen\360DesktopLite\360DesktopLite 64位\skin\未命名1.cpp:40: warning: passing `double’ for converting 1 of `void fund(int, void*)’
[Warning] D:\Program FilesGreen\360DesktopLite\360DesktopLite 64位\skin\未命名1.cpp:42:2: warning: no newline at end of file

构建中止 未命名1: 2 个错误, 6 个警告

有五种类型的标准转换,如下:

  • 从任何整数类型或枚举类型到其他整数类型(除了提升)
  • 从任何浮点类型到其他浮点类型(除了提升转换)
  • 从任何整数类型到浮点类型,或者反之
  • 整数0到指针类型、任何指针类型到 void 指针类型
  • 从任何整数类型、浮点类型、枚举类型、指针类型到bool
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值