Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is virtual before declaration and override after? [duplicate]

This is a design question, let's have a simple method:

virtual void Test() { };

We can override it the same way, or even not specifying virtual at all probably, but then there's this override keyword, which makes the compiler check it is actually overriding something, which is useful, but it needs to be written like this:

void Test() override { };

To me this makes no sense, since I'd edit literally thousands of these methods, and as it is now, the editing would be just too clumsy to spend the time with. So what is the logic behind placing the override after, since it could be much easier and to me generally better like this:

override void Test() { };
like image 675
mrzacek mrzacek Avatar asked Oct 14 '25 14:10

mrzacek mrzacek


1 Answers

virtual was introduced right at the beginning of C++ as a keyword. This means you can't use it as a variable name, a class name, a function name, and so on.

override came much later on. In order that its introduction in C++11 didn't break existing code, it doesn't quite attain the status of a keyword; rather it's called an identifier with special meaning. final is similar.

Its curious positioning is specified by the language grammar: an example where allowing it to be at the beginning would be a breaking change is

override :: foo bar()

where override::foo must be a qualified return type of the function bar() rather than an overrider with a explicitly global return type ::foo (Acknowledge @BenVoigt.)

like image 92
Bathsheba Avatar answered Oct 17 '25 02:10

Bathsheba