I've always wondered about the decision, why override
and final
has to be after member-function declarator:
struct Base {
virtual void virtFun();
};
struct Foo: Base {
virtual void virtFun() override;
};
For me, it would be more logical to put override
/final
in place of virtual
:
struct Base {
virtual void virtFun();
};
struct Foo: Base {
override void virtFun();
};
Is there a reason behind this? Maybe some compatibility issue with pre C++11?
The override specifier was introduced to the language with C++11 and it is one of the easiest tool to significantly improve the maintainability of our codebases. override tells both the reader and the compiler that a given function is not simply virtual but it overrides a virtual method from its base class(es).
The override keyword serves two purposes: It shows the reader of the code that "this is a virtual method, that is overriding a virtual method of the base class." The compiler also knows that it's an override, so it can "check" that you are not altering/adding new methods that you think are overrides.
override (C++11) final (C++11) [edit] Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be derived from.
Save this question.
It's because override
and final
are not keywords.
Instead they are special identifiers.
That means you can actually declare variables, functions or type-names (type-alias or classes) with those names.
They can only be used like member-function modifiers in a very small context, where the context have to be know beforehand by the compiler as it parses the source. Placing them after the function declaration is a very simple way of removing ambiguity from the C++ grammar for that context.
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