class CBase
{
public:
void Show()
{
std::cout << "CBase::Show()" << std::endl;
}
virtual void Print()
{
std::cout << "CBase::Print()" << std::endl;
}
};
class CCoo:public CBase
{
public:
void Show()
{
std::cout << "CCoo::Show()" << std::endl;
}
virtual void Print() override
{
std::cout << "CCoo::Print()" << std::endl;
}
};
std::shared_ptr<CBase> pBase = std::make_shared<CBase>();
pBase->Show();
pBase->Print();
std::shared_ptr<CCoo> pCCoo = std::make_shared<CCoo>();
pCCoo->Show();
pCCoo->Print();
std::shared_ptr<CBase> pCCoo2 = std::make_shared<CCoo>();
pCCoo2->Show();
pCCoo2->Print();
结果:
CBase::Show()
CBase::Print()
CCoo::Show()"
CCoo::Print()
CBase::Show()
CCoo::Print()
std::shared_ptr<CBase> pCCoo2 = std::make_shared<CCoo>();指向CBase的重写是使用CBase的类,而覆盖的Print函数是类CCoo