Does the override identifier after virtual destructor declaration have any special meaning?
class Base { public: virtual ~Base() {} virtual int Method() const {} }; class Derived : public Base { public: virtual ~Derived() override {} virtual int Method() override // error: marked override, but does not override - missing const {} };
Using override identifier on virtual method is useful as check: compiler will report error when the Base virtual method is actualy not overriden.
Does override on virtual destructor has any meaning/function too?
A pure virtual destructor can be declared in C++. After a destructor has been created as a pure virtual object(instance of a class), where the destructor body is provided. This is due to the fact that destructors will not be overridden in derived classes, but will instead be called in reverse order.
C++11 adds two inheritance control keywords: override and final. override ensures that an overriding virtual function declared in a derived class has the same signature as that of the base class. final blocks further derivation of a class and further overriding of a virtual function.
Function overriding is a redefinition of the base class function in its derived class with the same signature i.e. return type and parameters.
When you override a function you don't technically need to write either virtual or override . The original base class declaration needs the keyword virtual to mark it as virtual. In the derived class the function is virtual by way of having the ¹same type as the base class function.
Yes. If the base destructor is not virtual then the override
marking will cause the program to not compile:
class Base { public: ~Base() {} }; class Derived : public Base { public: virtual ~Derived() override //error: '~Derived' marked 'override' but does // not override any member functions {} };
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