Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override as default specifier...why not?

Tags:

c++

c++11

I've been noticing since using c++11, that in classes deriving from interfaces I literally always use the override specifier (as to me one would mostly want to know when a virtual signature in the base changes). I almost feel as if it's wrong to continuously specify override, which brings me to my question:

Why did the standards committee not decide to make override the default (generating a compiler error on base signature change for virtuals), whilst adding a keyword to explicitly "not caring" e.g. override_ignore (not that I think one would ever want to ignore this)?

like image 781
Werner Erasmus Avatar asked Oct 23 '25 15:10

Werner Erasmus


2 Answers

As mentioned by Nathan Oliver: backwards compatibility (a necessary evil). Although it's fairly easy to do a mass-replace on the term 'override' in a codebase, it would simply serve as another barrier to an already-reluctant programming community.

Another point to make is code readability. If you're working on maintaining a small part of a very large codebase, it's nicer to have the override specifier within your child classes to remind you that:

  • the method shouldn't be refactored without refactoring the parent class and all sibling classes,
  • the method is there to fulfil a specific requirement, and
  • that any information about the necessary working of the method can be found in the parent class' header.

C++ isn't focused on making the code itself as small and efficient as possible - that's why it rarely wins any code-golf challenges. Instead, it is a language which is focused on rigour and being explicit in everything you do. At least, that's my take on the language, and why (although some common operations require more time to write out than in many other languages) it's my go-to language when I need clarity within complex programs.

like image 58
Jake Avatar answered Oct 26 '25 05:10

Jake


Consider also the following case:

struct A {
    virtual void foo();
}

struct B: public A {
    virtual void fog() override; 
}

You mistyped the function name (fog instead of foo), and this code will give you compile error. However, with your override-by-default approach, the compiler will never know that there is an error.

like image 39
Petr Avatar answered Oct 26 '25 06:10

Petr