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?
Can We Override a Final Method? No, the Methods that are declared as final cannot be Overridden or hidden.
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.
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.
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.
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 classB
is marked with the virt-specifierfinal
and in a classD
derived fromB
a functionD::f
overridesB::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.
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.
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