您的位置:首页 > 编程语言 > C语言/C++

C/C++ 函数重载

2009-09-05 16:48 155 查看
#include <iostream>
using namespace std;
void test(int i,int j=0)
{
}
void test(int i){}
int main()
{
//test(1);
return 0;
}


上面的程序编译可以通过,但是在main函数中调用test(1)的时候会出现ambiguous,编译器不知道该调用上面的那个函数。

#include <iostream>
using namespace std;
void test(int* i){}
void test(const int* i){}
int main()
{
return 0;
}


上面的程序也可以编译通过,由此可以认为const int*和int*属于不同的类型,但是如果我把程序改成下面这样:

#include <iostream>
using namespace std;
void test(int* i){}
void test(int* const i){}
int main()
{
return 0;
}


则编译不通过,因为此时const只是一个修饰符而已。

下面的程序也可以编译通过,并且函数调用的结果是no const function!

#include <iostream>
using namespace std;
class c
{
public:
void test(int i)const
{
cout<<"const function!"<<endl;
}
void test(int i)
{
cout<<"no const function!"<<endl;
}
};
int main()
{
c t;
t.test(1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: