Method of calling base class virtual function in c++

The following shows an example:

#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(); //Add domain name to call return 0; }




Running result:
[root@localhost code]# ./pdata
Test ---- //Call the subclass
Base— //Call the base class

Guess you like

Origin blog.csdn.net/weixin_44881103/article/details/107244785