64,643
社区成员
发帖
与我相关
我的任务
分享
//程序1:
#include <iostream>
using std::cout;
using std::endl;
void ff(int a,float b,float c, float d)
{
cout << "f1" << endl;
}
void ff(double a, int b, double c, int d)
{
cout << "f2" << endl;
}
void main()
{
int i = 1, j = 2, k = 3, l = 4;
ff(i, j, k, l);
}
//程序2:
#include <iostream>
using std::cout;
using std::endl;
void ff(float a,int b,float c, float d) //程序1中的前两个参数调换了下顺序
{
cout << "f1" << endl;
}
void ff(double a, int b, double c, int d)
{
cout << "f2" << endl;
}
void main()
{
int i = 1, j = 2, k = 3, l = 4;
ff(i, j, k, l);
}
void fun(float f) {}
void fun(double d) {}
void main()
{
int i;
fun(i);//二义性错误
}
[/quote]
4个参数后者更加匹配吧,因为int
就像下面这两种情况会匹配到有int类型变量的那个函数:
void fun(float,int) {}
void fun(double,double) {}
void main()
{
int i;
fun(i,i);
}
void fun(float,float) {}
void fun(double,int) {}
void main()
{
int i;
fun(i,i);
}
void fun(float f) {}
void fun(double d) {}
void main()
{
int i;
fun(i);//二义性错误
}