This has probably been asked before on SO, but I was unable to find a similar question.
Consider the following class hierarchy:
class BritneySpears { public: virtual ~BritneySpears(); }; class Daughter1 : public BritneySpears { public: virtual ~Daughter1(); // Virtual specifier }; class Daughter2 : public BritneySpears { public: ~Daughter2(); // No virtual specifier };
Is there a difference between Daughter1
and Daughter2
classes ?
What are the consequences of specifying/not specifying virtual
on a sub-class destructor/method ?
It is not mandatory for the derived class to override (or re-define the virtual function), in that case, the base class version of the function is used.
Derived classes do not have to implement all virtual functions themselves. They only need to implement the pure ones. That means the Derived class in the question is correct.
No, the virtual keyword on derived classes' virtual function overrides is not required.
The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defined unless they are declared using the pure-specifier.
No you technically do not need to specify virtual
. If the base method is virtual then C++ will automatically make the matching override method virtual
.
However you should be marking them virtual
. The method is virtual
after all and it makes your code much clearer and easier to follow by other developers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With