父类与子类中的重名问题(shadow)
#include "stdafx.h"
#include <iostream>
using namespace std;
#if 0
//shadow只和函数名有关(函数名相同即可shadow)
//与函数的返回值、参数等均无关
overload重载发生在 同一作用域 函数名相同
参数列表不同(个数,类型,顺序) (希望发生)
shadow 反生在父子类当中(爷爷类等),只要函数名相同,
即可构成shadow (不希望发生)
#endif
class Father
{
public:
void dis()
{
cout << "Father" << endl;
}
};
class Son :public Father
{
public:
void dis() //shadow 子类中会把父类中重名的成员shadow
{
//dis(); //绝不要使子类中的成员同父类中的成员同名
Father::dis(); //非要重名使用则需要加命名空间
cout << "Son" << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Son son;
//son.dis(); 重名会一直递归 调用自己 直至崩溃
son.dis();
return 0;
}