在C++中 - 派生类从基类派生,并且派生类正在重写的基类中有一个虚方法。有人能告诉我一个真实的场景,虚拟函数的派生类版本可能需要调用虚函数的基类版本吗?通过派生类虚拟方法调用基类虚拟方法
例,
class Base
{
public:
Base() {}
virtual ~Base() {}
virtual void display() { cout << "Base version" << endl; }
};
class Derived : public Base
{
public:
Derived() {}
virtual ~Derived() {}
void display();
};
void Derived::display()
{
Base::display(); // a scenario which would require to call like this?
cout << "Derived version" << endl;
}
任何情况下,派生类都在扩展基类的功能,所以不是重复基类的功能,而是调用它,然后执行其他步骤在派生类中。 – Praetorian