重载(overload) :同一作用域中(函数调用作用域,而不是函数声明作用域),函数名相同,参数表不同的函数。函数指针的重载类型由函数指针参数决定匹配的重载函数
覆盖(override) :
重写(overwrite):
#include <iostream>
using namespace std;
namespace ns1
{
void fun(int n)
{
cout << "ns1::fun" << endl;
}
}
namespace ns2
{
void fun(double n)
{
cout << "ns2::fun" << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
using namespace ns1;
using namespace ns2;
fun(3);//ns1::fun
fun(3.14);//ns2::fun
using ns1::fun;
fun(3);//ns1::fun
fun(3.14);//ns1::fun
using ns2::fun;
fun(3);//ns1::fun
fun(3.14);//ns2::fun
void (*pFun)(double) = fun;
pFun(3);//ns2::fun
getchar();
return 0;
}
注意:1)以下构成了重载关系,但是编译器编译不过去,因为重载是依靠的换名机制
extern "C"
{
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a = b; }
}
2)注意不要因为使用缺省参数而导致重载歧义
3)只指定类型不指定名称的函数参数叫做哑元,也可以行程重载版本,对于i++和++i可以用到哑元
#include <iostream>
using namespace std;
void fun(int)
{
cout << "fun(int)" << endl;
}
void fun(double d)
{
cout << "fun(double d)" << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
fun(3);//fun(int)
fun(3.14);//fun(double d)
getchar();
}