本文共 1976 字,大约阅读时间需要 6 分钟。
#include int foo() { return 10;}char foo() { return 'a';}int main(){ char x = foo(); getchar(); return 0;}
2) 成员函数名称以及参数完全相同,仅仅其中一个是static成员函数。 #include class Test { static void fun(int i) {} void fun(int i) {}};int main(){ Test t; getchar(); return 0;}
3) 函数参数仅仅是指针与数组的差别。这样实际上是相等的。 int fun(int *ptr);int fun(int ptr[]); //相当于fun(int *ptr)的重复声明
4) 函数差别仅在于参数类型,其中一个是函数类型,另一个是相同类型的函数指针。这样实际上是相等的。 void h(int ());void h(int (*)()); //相当于h(int())的重复声明
如果只是这样的声明两个函数,也许可以编译通过。但是,写上函数主体后,如下所示,编译会报错。
void h(int()){}void h(int(*)()){}
visual studio 2015编译器提示:“error C2084: 函数“void h(int (__cdecl *)(void))”已有主体”
5) 函数参数差别仅在于有无const或volatile。 当编译器决定哪个函数被声明,定义或调用时,每个参数的const和volatile类型修饰会被忽略掉。
例如,下面的程序会编译失败,提示error “redefinition of `int f(int)’ ”
#include #include using namespace std;int f ( int x) { return x+10;}int f ( const int x) { return x+10;}int main() { getchar(); return 0;}
只有当const和volatile是在参数类型修饰的最外层(左侧)使用时,编译器才会进行区别;如果const和volatile被包围在了参数类型修饰的里边,则不能用来区分重载函数声明。特别地,对于任意类型T, “pointer to T,” “pointer to const T,” and “pointer to volatile T” 可以用来区别参数类型, 后面的引用也同样如此, “reference to T,” “reference to const T,” and “reference to volatile T.” 具体参加下面例子:
void f(const int *x)或void f(int const *x) // 编译正常
但是,第二个不能为void f(int * const x)
void f(volatile int *x)或void f(int volatile *x) // 编译正常
但是,第二个不能为void f(int * volatile x)
void f(const int& x)或void f(int const &x) //编译正常
但是,第二个不能为void f(int & const x) //这样写本身就不合法
6) 函数之间的差别仅在于默认参数值的不同。这样实际上也是相等的. 例如,下面程序会编译错误“redefinition of `int f(int, int)’ “
#include #include using namespace std;int f ( int x, int y) { return x+10;}int f ( int x, int y = 10) { return x+y;}int main() { getchar(); return 0;}
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
转载地址:http://qmeji.baihongyu.com/