Should I always use the override contextual keyword?
Asked Answered
K

3

7

I know that the override contextual keyword was introduced to write safer code (by checking for a virtual function with the same signature) but I don't feel good about it, because it seems to be redundant for me to write override every time I want to override a virtual function.

Is it a bad practice to not use override contextual keyword for 99% of cases? Why/when should I have to use it (a compiler warning is not enough when we are hiding a virtual function mistakenly)?

EDIT: In other words; what is the advantage of using the override contextual keyword in C++11 while we always had a compiler warning if we were hiding a virtual function mistakenly in C++03 (without using override contextual keyword)?

Klemens answered 30/7, 2014 at 10:30 Comment(1)
Can you explain why you think it is redundant?Vaccinate
F
9

The override keyword is totally useful and I would recommend using it all the time.

If you misspell your virtual function it will compile fine but at runtime the program will call the wrong function. It will call the base class function rather than your override.

It can be a really difficult bug to find:

#include <iostream>

class Base
{
public:
    virtual ~Base() {}

    virtual int func()
    {
        // do stuff for bases
        return 3;
    }
};

class Derived
: public Base
{
public:

    virtual int finc() // WHOOPS MISSPELLED, override would prevent this
    {
        // do stuff for deriveds
        return 8;
    }
};

int main()
{
    Base* base = new Derived;

    std::cout << base->func() << std::endl;

    delete base;
}
Falchion answered 30/7, 2014 at 11:23 Comment(1)
and also note that is probably cleaner to omit the now redundant virtual, i.e. int finc() overrideTropism
D
3

Annotations are what you call contextual keywords, they serve as clarification, to make sure anyone who reads the code realizes it is a function that overrides a function in a superclass or a interface. The compiler can also give a warning if the originally overridden feature was removed, in which case you might want to think about removing your function as well.

As far as I know, nothing bad happens if you ommit anotations. It's neither right nor wrong. Like you stated correctly already: annotations are introduced to write safer code. However: They won't change your code in any functional way.

If you work as a single programmer on your own project it might not matter wheter you use them or not. It is however good practice to stick to one style (i.e. either you use it, or you don't use it. Anything inbetween like sometimes using it and sometimes not only causes confusion)

If you work in a Team you should discuss the topic with your teammates and decide wheter you all use it or not.

Demetrius answered 30/7, 2014 at 10:43 Comment(3)
I need to point out that I discovered the c++ tag (and the edit about c++) after I posted my answer. I first thought it was all about Java. However, the way I see it the concept for annotations are the same for both languages alike. so it should not matter. But maybe someone wants to confirm my answer or edit if deemed necessary.Demetrius
"The compiler can also give a warning if the originally overridden feature was removed", but in C++ it is a compiler error. BTW you are right, using override keyword will increase code readability but i am wonder why did C++ community add a keyword only for readability!? :-PKlemens
We invented C C++ etc. because reading/writing machine code (zeros and ones) or assembly code is a hassle. Programming languages are invented for one purpose: to make programming easier (and more readable). And every programming language has a different concept of what is needed to make that happen. =) BTW 10% of the time a computing scientist creates new code, 20% of the time he modifies existing code, and finally 70% of his time he is trying to figure out what already existing code is actually doing. So yeah readability does make a difference in 90% of cases.Demetrius
S
1

What is the advantage of using override contextual keyword in C++11 while we always had a compiler warning if we were hiding a virtual function mistakenly

Nearly none!?

But: It depends on how much warnings will be accepted by your build rules. If you say, every warning MUST be fixed, you will get the same result UNTIL you are using a compiler which give you the warning.

We have decided to always use override and remove virtual on overriding methods. So the "overhead" is zero and the code is portable in the meaning of "give an error" on misuse.

I personally like this new feature, because it makes the language clearer. If you say this is an override, it will be checked! And if we want to add a new method with different signature, we will NOT get a false positive warning, which is important in your scenario!

Sing answered 30/7, 2014 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.