Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In final class derived from base class with virtual destructor, does derived class destructor need “virtual” keyword? Should it have “final” keyword?

Tags:

c++

c++17

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?
like image 642
excommunicado Avatar asked Oct 18 '25 12:10

excommunicado


1 Answers

  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.

like image 112
Ben Voigt Avatar answered Oct 21 '25 02:10

Ben Voigt