6

I understand the purpose of the C++ override, however am a bit confused by it's implementation compared to other higher level languages, where its use is required by default.

The C++11 wiki page describes it as a "technically identifier for declarator attribute" but does not elaborate as to why it is not simply a keyword for the language.

nonathaj
  • 63
  • 2
  • 4

2 Answers2

10

It is optional to maintain backwards compatibility with C++03. Making it non-optional would have broken all the code*.

Similarly, making override a keyword would have broken any code that used the name override.


OK, not literally all the code, but a lot of it.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 1
    Ah, I didn't realize that backwards compatibility was such a high goal with C++11, but it is [literally the first design goal](https://en.wikipedia.org/wiki/C%2B%2B11#Design_goals) that the committee listed. – nonathaj Mar 24 '16 at 22:08
  • @nonathaj Yes, if it wasn't for that, C++ would be a great deal less awful :-) – juanchopanza Mar 24 '16 at 22:10
  • 1
    Some compilers, such as Clang will detect that "you are using `override` in some of your code, so I'll warn you when it's missing in other places". In other words, it tries to enforce a consistent use of it. – Mats Petersson Mar 24 '16 at 22:33
  • @juanchopanza Off-topic but, to be honest, if it wasn't backward compatible, we'd still be dealing with C++98-only legacy codebases for the next ten years... Sure, new codebases would be less awful if we had "broken a few eggs", but I think what we have right now is the best compromise. Well, until we get perfect refactor tools that can update your codebase for free – KABoissonneault Mar 25 '16 at 02:04
  • @KABoissonneault Sure, and I wasn't trying to suggest that breaking backwards compatibility was ever a practical option. It just explains a lot of the weirdness in the language. – juanchopanza Mar 25 '16 at 06:15
  • 1
    clang-modernize has an option to automatically add override in your code base. – sbabbi Mar 25 '16 at 10:28
3

Technically, C++11 does not behave much differently from Java here (which is a typical example for one of the "other higher level languages" which you mention). A wrong override will be a compilation error, just like a wrong @Override in Java. A missing override will not be a compilation error, just like a missing @Override will not be a compilation error in Java.

The only real difference I can see is that Java tools have traditionally had better support to detect a missing @Override, and that Java users are traditionally encouraged to treat the corresponding warning as an error, whereas C++ compilers have been quite slow so far at adding warning options for missing overrides.

But we're getting there; Clang now has -Winconsistent-missing-override, and newer GCCs have -Wsuggest-override. All you have to do is enable those warnings and treat them as errors, either forcibly by the compiler or simply by convention.

As for why it's not simply a keyword: backward compatibility with older code.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62