重载定义
如果两个函数名字相同并且在相同的域中被声明,但是参数表不同,则它们就是重载函数。
重载函数条件
2.1、参数类型或参数个数不同
2.2、返回值不同不能视为重载
2.3、是否为常函数不能视为重载
2.4、对于普通类型参数只有const、volatile区别不能视为重载
实例
3.1、以下几种情况会编译失败
#include<iostream>
int foo() {
return 10;
}
char foo() {
return 'a';
}
int main()
{
char x = foo();
getchar();
return 0;
}
#include<iostream>
class Test {
static void fun(int i) {}
void fun(int i) {}
};
int main()
{
Test t;
getchar();
return 0;
}
#include<iostream>
#include<stdio.h>
using namespace std;
int f ( int x) {
return x+10;
}
int f ( const int x) {
return x+10;
}
int main() {
getchar();
return 0;
}
3.2、以下几种情况函数等价
int fun(int *ptr);
int fun(int ptr[]); // redeclaration of fun(int *ptr)
void h(int ());
void h(int (*)()); // redeclaration of h(int())
参考资料:
(http://www.geeksforgeeks.org/function-overloading-in-c/)
(http://www.cnblogs.com/skynet/archive/2010/09/05/1818636.html)