Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does final imply override?

As I understand it, the override keyword states that a given declaration implements a base virtual method, and the compilation should fail if there is no matching base method found.

My understanding of the final keyword is that it tells the compiler that no class shall override this virtual function.

So is override final redundant? It seems to compile fine. What information does override final convey that final does not? What is the use case for such a combination?

like image 395
quant Avatar asked Apr 02 '15 11:04

quant


People also ask

Can final function override?

Can We Override a Final Method? No, the Methods that are declared as final cannot be Overridden or hidden.

What is override final C++?

Virtual, final and override in C++ Published February 21, 2020. C++11 added two keywords that allow to better express your intentions with what you want to do with virtual functions: override and final . They allow to express your intentions both to fellow humans reading your code as well as to the compiler.

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.

What does final keyword mean in C++?

Final keyword in C++ when added to a function, prevents it from being overridden by derived classes. Also when added to a class prevents inheritance of any type.


2 Answers

final does not require the function to override anything in the first place. Its effect is defined in [class.virtual]/4 as

If a virtual function f in some class B is marked with the virt-specifier final and in a class D derived from B a function D::f overrides B::f, the program is ill-formed.

That's it. Now override final would simply mean
„This function overrides a base class one (override) and cannot be overriden itself (final).“
final on it's own would impose a weaker requirement. override and final have independent behavior.


Note that final can only be used for virtual functions though - [class.mem]/8

A virt-specifier-seq shall appear only in the declaration of a virtual member function (10.3).

Hence the declaration

void foo() final; 

Is effectively the same as

virtual void foo() final override; 

Since both require foo to override something - the second declaration by using override, and the first one by being valid if and only if foo is implicitly virtual, i.e. when foo is overriding a virtual function called foo in a base class, which makes foo in the derived one automatically virtual. Thus override would be superfluous in declarations where final, but not virtual, occurs.
Still, the latter declaration expresses the intent a lot clearer and should definitely be preferred.

like image 108
Columbo Avatar answered Oct 01 '22 07:10

Columbo


final does not necessarily imply that the function is overridden. It's perfectly valid (if of somewhat dubious value) to declare a virtual function as final on its first declaration in the inheritance hierarchy.

One reason I can think of to create a virtual and immediately final function is if you want to prevent a derived class from giving the same name & parameters a different meaning.

like image 44
Angew is no longer proud of SO Avatar answered Oct 01 '22 07:10

Angew is no longer proud of SO