c++中调用基类虚函数的方法

以下展示实例:

#include

using namespace std;

class Base{
public:
virtual void file()
{
cout << “Base—” << endl;
}
};

class Test:public Base{
public:
virtual void file()
{
cout << “Test ----” << endl;
}
};

int main()
{
Base *b=new Test();
b->file();
b->Base::file(); //添加域名来调用
return 0;
}

运行结果:
[root@localhost code]# ./pdata
Test ---- //调用子类
Base— //调用基类

猜你喜欢

转载自blog.csdn.net/weixin_44881103/article/details/107244785