Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override identifier after destructor in C++11

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?

like image 756
EnterTheNameHere Bohemian Avatar asked Jul 29 '13 11:07

EnterTheNameHere Bohemian


People also ask

Can destructor be overridden in C?

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.

What is override in C++11?

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.

What is override specifier C++?

Function overriding is a redefinition of the base class function in its derived class with the same signature i.e. return type and parameters.

Does override imply virtual?

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.


1 Answers

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     {} }; 
like image 174
Mankarse Avatar answered Sep 23 '22 03:09

Mankarse