class继承,子类继承父类函数,部分继承函数被遮掩,无法调用
C++的名称遮掩规则所做的唯一事情就是:遮掩名称。至于名称是否应和相同或不相同的类型,并不重要。
Derived继承Base,当编译器看到名称f,必须估算它指涉什么东西。编译器的做法是查找作用域,看看有没有某个名为f的声明式。查找作用域顺序:local(Derived)---Base---namespace(包含Base)---global.
使用using Base::f,添加所有名为f的东西到Derived内;
class Dervied: public Base { public: using Base::f; //添加,所有Base内的所有f都会在Derived作用域内可见 void f(); virtual void vf(); }; int main() { Dervied d; int x = 0; d.f(); //调用Derived::f d.f(10); //没有问题Base::f(int) d.vf() //调用Derived::vf d.vf1(); //调用Base::vf1 system("pause"); }不添加所有,转交函数,只使用部分
class Dervied: private Base { //private继承 public: //using Base::f; void f(); virtual void vf() //转交函数 { Base::vf(); } }; void Dervied::f() {} int main() { Dervied d; int x = 0; d.f(); //调用Derived::f //d.f(10); //错误,因为Derived::f遮掩了Base::f d.vf(); //调用Derived::vf //d.vf1(); //private继承,Base::vf1不可访问 system("pause"); }