Why was the redefinition shown after the participant was declared instead of the same place as the virtual one?

I'm curious why the new C ++ 11 override forced to appear after declaring a method according to const instead of virtual ?

 class SomeBaseClass { virtual void DoPolymorphicBehavior() = 0; ... class SomeDerrivedClass : public SomeBaseClass { void DoPolymorphicBehavior() override; ... 

Why the world does not allow it in the same exact position (and even instead) virtual

 class SomeBaseClass { virtual void DoPolymorphicBehavior() = 0; ... class SomeDerrivedClass : public SomeBaseClass { override void DoPolymorphicBehavior(); ... 

This would allow me to search and replace my source files for derived classes in order to trivially use the new keyword and thus get help from the compiler in finding errors. However, since C ++ 11 syntactically puts it in a different position, I would have to manually update literally several thousand lines of source code to benefit from the new compiler function.

Is there really a good reason for this choice?

+6
source share
3 answers

The sequence of the declaration qualifier that appears before the function name may contain an identifier (for example, the return type of a function). Imagine any existing code had an override return type:

 override foo(); 

Or even a variable called override :

 int override; 

Introducing a new override keyword will break any existing code that contains an identifier named override because the keywords are reserved.

So instead of entering a new keyword, they entered a contextual keyword: override (as well as final ). A contextual keyword is identified as a keyword by its syntactic position. It is still good to have identifiers called override and final in your program. If these identifiers appear after the argument list in the function declaration, they have special meaning.

So, the reason that it stands after the function arguments is that introducing new keywords will break the old code, and if the compiler sees an override here, they know exactly what this means, since there can be no other identifier.

+10
source

This is not a keyword, and it is also the answer to your question.

This is an identifier with a special meaning in some contexts. If it is allowed to appear at the beginning of the declaration, it can be ambiguous, for example, with the user name specified by the user.

+7
source

Because override and final are not keywords, but symbols that can be displayed in user code. (Ie you can have an int override; variable int override; ). They only take their special meaning in restricted contexts, and these contexts should be selected to match the context in which a custom character cannot appear. The desire was also to make this immediately clear; in for example:

 override void DoSomething(); 

override cannot be a user character because there is no grammar production that could have a statement starting with a user character followed by void . But the problem is not until the compiler encounters void , and if instead of void you have a user-defined type, it is even more ambiguous. On the other hand, after proposing a parameter-declaration of a function, the context is clear there, and the scanner compiler immediately knows what to do.

+2
source

All Articles