Home > Software engineering >  In final class derived from base class with virtual destructor, does derived class destructor need “
In final class derived from base class with virtual destructor, does derived class destructor need “

Time:12-16

I have a Base class with virtual destructor and final Derived inheritor:

class Base {
public:
  virtual ~Base() { 
    // some impl
  }
};

class Derived final : public Base {
public:
  virtual ~Derived() override {
    // some other impl
  }
};

I have the following questions:

  • Does ~Derived needs virtual specifier?
  • Should I mark ~Derived as final?

CodePudding user response:

  1. ~Derived() will be a virtual destructor, whether you apply the keyword or not. That's because the base class destructor is virtual.

  2. ~Derived() will be a final overrider, because it is a member of a class which is marked final. It does not matter whether you use the final keyword directly on the destructor or not.

  • Related