We know that the base class destructor is marked virtual for proper destruction of derived class instances as the base type pointers. Then what are the differences between the following derived class destructor practices?
- Simple destructor:
~Derived();
- Destructor with override:
~Derived() override;
- Default destructor with override:
~Derived() override = default;
- Virtual destructor:
virtual ~Derived() override = default;
Also is there a known best practice?
override
just gives you compiler checks. 3. and 4. also same.virtual
is derived to child classes. It's not necessary in child classes.virtual
is redundant ifoverride
is set. IMHO dont declare a destructor if you don't need it, declare a destructor with
override` anddefault
if you need a declaration and declare a destructor withoverride
if you need a custom destructor. – Thomas Sablik