查找名字时,编译器先从子类开始,一旦找到了,就不再继续查找。无论普通函数,虚函数,还是纯虚函数,结果都是输出子类的函数调用。 避免遮掩继承而来的名称是因为,你在使用public继承而又不继承那些重载函数,就是违反base和derive class之间的 is 关系。
如果非要访问父类的方法,用两种方法可以实现
1. 使用using Base::func();声明
2. 定义转交函数
//:采用using声明,使查找范围扩大至父类指定的函数:
class Base
{
public:
void CommonFunction(){cout << "Base::CommonFunction()" << endl;}
void virtual VirtualFunction(){cout << "Base::VirturalFunction()" << endl;}
void virtual VirtualFunction(int x){cout << "Base::VirtualFunction() With Parms" << endl;}
void virtual PureVirtualFunction() = 0;
};
class Derived: public Base
{
public:
using Base::VirtualFunction; // 第一级查找也要包括Base::VirtualFunction
void CommonFunction(){cout << "Derived::CommonFunction()" << endl;}
void virtual VirtualFunction(){cout << "Derived::VirturalFunction()" << endl;}
void virtual PureVirtualFunction(){cout << "Derived::PureVirtualFunction()" << endl;}
};
int main()
{
Derived d;
d.VirtualFunction(3); // 这样没问题了,编译器会把父类作用域里面的函数名VirtualFunciton也纳入第一批查找范围,这样就能发现其实是父类的函数与main中的调用匹配得更好(因为有一个形参),这样会输出Base::VirtualFunction() With Parms
return 0;
}
//:使用转交函数强制指定父类的作用域
class Base
{
public:
void CommonFunction(){cout << "Base::CommonFunction()" << endl;}
void virtual VirtualFunction(){cout << "Base::VirturalFunction()" << endl;}
void virtual VirtualFunction(int x){cout << "Base::VirtualFunction() With Parms" << endl;}
void virtual PureVirtualFunction() = 0;
};
class Derived: public Base
{
public:
using Base::VirtualFunction;
void CommonFunction(){cout << "Derived::CommonFunction()" << endl;}
void virtual VirtualFunction(){cout << "Derived::VirturalFunction()" << endl;}
void virtual PureVirtualFunction(int x){cout << "Derived::PureVirtualFunction()" << endl;}
void virtual VirtualFunction(int x){Base::VirtualFunction(x)};
};
int main()
{
Derived d;
d.VirtualFunction(3); // 输出Base::VirtualFunction() With Parms
return 0;
}
总结: 派生类的名字会遮盖基类的名字
为了让遮盖的名字重见天日,可以用using声明或者转交函数。