遇到这样一道题,注释以及很清楚,覆盖是覆盖了,但是可以用using指令使其可见,并成功调用,注意灰显部分。
#include <iostream>
using namespace std;
class Base{
public:
virtual void func(){cout<<"Base::func()"<<endl;}
void gunc(){cout<<"Base::gunc()"<<endl;}
};
class Derived:public Base{
public:
void func(){cout<<"void Derived::func()"<<endl;}//覆盖文类中的函数,类型,函数名,返回类型必须相同
int func()const{cout<<"int Derived::func() const"<<endl;return 1;} //const类型不用可以做为重载
int gunc(int x, int y){cout<<"void Derived::func(int, int)"<<endl;} //隐藏父类中的同名函数
//using Base::gunc;
};
void test(){
Base *pb = new Derived;
pb->func(); // void Derived::func()
Derived d;
d.gunc(); //这样会出错,因为父类中的函数被隐藏了,子类不存在参数类型为void的函数;
//d.gunc(1,0); // void Derived::func(int, int)
const Derived d2;
d2.func(); // int Derived::func()
}
int main()
{
test();
return 0;
}