I'm trying to see the reason why, in C++11, they had to add the override
keyword at the end of the method instead of the beginning like virtual
. I don't see the interest of being able to write both virtual
and override
in the declaration of a method.
Is there a technical reason why the committee didn't choose to simply be able to write override
instead of virtual
when it was needed?
Thanks!
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.
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.
override Keyword in C++ C++Server Side ProgrammingProgramming. The function overriding is the most common feature of C++. Basically function overriding means redefine a function which is present in the base class, also be defined in the derived class.
The override special identifier means that the compiler will check the base class(es) to see if there is a virtual function with this exact signature. And if there is not, the compiler will error out.
The proposal for the addition of the keywords controlling override (override
/final
) , paper N3151 , gives us some insight about this choice (emphasis mine) :
It is preferable to put such virtual control keywords at the end of the declaration so that they don't clash with eg. return types at the beginning of declarations.
[...]
For context-insensitive, normal keywords, it's less important where the keywords are placed because the words are reserved. We could put them at the beginning of declarations or at the end.
During the discussion of attributes, Francis Glassborow pointed out that the beginning of declarations is becoming crowded. If we put the virtual control keywords at the beginning, we can end up with examples like the one below:
struct B
{
virtual volatile const unsigned long int f()
volatile const noexcept;
void f(int g);
};
struct D : B
{
virtual hides_name virtual_override final_overrider volatile const unsigned long int f()
volatile const noexcept;
};
Putting the new keywords at the end at least alleviates the situation somewhat:
struct B
{
virtual volatile const unsigned long int f()
volatile const noexcept;
void f(int g);
};
struct D : B
{
virtual volatile const unsigned long int f()
hides_name virtual_override final_overrider volatile const noexcept;
};
There are people who think these control keywords should be in the same place with virtual. As mentioned, that place is already crowded.
Note:
The C++ 11 Standard defines context sensitive keywords in section § 2.11 / 2 [lex.name] :
The identifiers in Table 3 have a special meaning when appearing in a certain context. When referred to in the grammar, these identifiers are used explicitly rather than using the identifier grammar production. Unless otherwise specified, any ambiguity as to whether a given identifier has a special meaning is resolved to interpret the token as a regular identifier.
Table3:
final override
There certainly is a technical reason for it! You can read all about it in this article.
Briefly, override
is a context-sensitive keyword, which means that you can also use it as an identifier. It was done this way to avoid breaking existing code that uses this identifier. That means that it has to appear in a position where identifiers are not allowed, namely immediately after the closing parenthesis of a function declaration.
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