【c++ primer, 5e】函数匹配

练习

6.49

候选函数:与所调用的函数的名字相同的函数的集合。

可行函数:给候选函数加上参数数量、参数类型的约束所得到的函数的集合。

6.50

a 3、4可行,二义匹配

b 2、4可行,2是最佳匹配

c 3、4可行,3是最佳匹配

d 3、4可行,4是最佳匹配

6.51

测试代码:

#include <iostream>
using namespace std;
void f()
{
    cout << "f1" << endl;
}
void f(int)
{
    cout << "f2" << endl;
}
void f(int, int)
{
    cout << "f3" << endl;
}
void f(double, double = 3.14)
{
    cout << "f4" << endl;
}
int main()
{
    // f(2.56, 42);  error: call of overloaded 'f(double, int)' is ambiguous
    f(42);
    f(42, 0);
    f(2.56, 3.14);
    return 0;
}

输出结果:

$ a
f2
f3
f4

【实参类型转换】

练习

6.52

a 3

b 4

6.53

c 是不合法的,详细参考p208。

原文地址:https://www.cnblogs.com/xkxf/p/6658221.html