23

Is it dangerous to use both virtual and override on a function in C++? Does that open you up for ambiguity with overloading?

Obviously virtual must be used in the base class and it would be silly to not use override in the derived class, but is it actually problematic to use virtual with override in the derived class?

Trying to determine if this is an issue of style or correctness.

Example:

class Widget {
  virtual void transmogrify() = 0;
}

class Gadget : public Widget {
  virtual void transmogrify() override {}
}
Rapptz
  • 20,807
  • 5
  • 72
  • 86
Mark
  • 3,806
  • 3
  • 21
  • 32
  • You might find your answer here [\`override\` in c++11](http://stackoverflow.com/questions/13880205/override-in-c11) – MicroVirus Apr 20 '15 at 23:44
  • 1
    Also see http://en.cppreference.com/w/cpp/language/override – MicroVirus Apr 20 '15 at 23:46
  • Overloading doesn't come into it. Both keywords only apply to over-riding. – user207421 Apr 20 '15 at 23:47
  • @EJP - One thing the override keyword buys you is protection from accidentally overloading instead of overriding. If I had a function in my base class that took an int argument and in my derived class I have a function with the same name but it takes a double, things get interesting. `override` protects you from such confusion. If you declared the dervied class' function `virtual` (and not `override`), that simply declares a new virtual function, oops. – Mark Apr 20 '15 at 23:49
  • 1
    @Mark I'm aware of that. I was referring to your query 'does that open you up for ambiguity with overloading?' – user207421 Apr 20 '15 at 23:55
  • 2
    @Mark: But "new virtual function" does not overload, it hides. To overload, you'd need an overrider or using statement for the existing function, plus a new one. – Ben Voigt Apr 20 '15 at 23:57

3 Answers3

35

The virtual keyword has no effect when you are overriding. A derived function that is a signature match for a virtual function defined in a base class will override the base definition, and the override will be entered in the vtable, whether the virtual keyword is used in the derived class or not.

Because the override keyword will cause a compile error if overriding is not happening, the virtual keyword is useless in combination.

Here, have a cheatsheet:

| Keyword used | Matching virtual function in base class | Result                   |
|--------------|-----------------------------------------|--------------------------|
| Neither      | No                                      | New non-virtual function |
| Neither      | Yes                                     | Override                 |
| virtual      | No                                      | New virtual function     |
| virtual      | Yes                                     | Override                 |
| override     | No                                      | Compile error            |
| override     | Yes                                     | Override                 |
| Both         | No                                      | Compile error            |
| Both         | Yes                                     | Override                 |
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
3

Late to the game, but this C++ Core Guideline seems relevant here:

C.128: Virtual functions should specify exactly one of virtual, override, or final

Reason

Readability. Detection of mistakes. Writing explicit virtual, override, or final is self-documenting and enables the compiler to catch mismatch of types and/or names between base and derived classes. However, writing more than one of these three is both redundant and a potential source of errors.

It's simple and clear:

  • virtual means exactly and only "this is a new virtual function."
  • override means exactly and only "this is a non-final overrider."
  • final means exactly and only "this is a final overrider."
mindriot
  • 5,413
  • 1
  • 25
  • 34
  • "virtual means exactly and only this is a new virtual function." is a style convention -- it does not mean that to the compiler – Ben Voigt Sep 21 '19 at 13:23
1

In your example you are saying two different, but important things about Gadget's method transmogrify

virtual - if a class derives from Gadget then the transmogrify function will be treated as virtual by the derived class

override - the Gadget class is explicitly overriding the base class Widget's version of transmogrify.

The two key works are orthogonal and do not affect one another. The nice thing about the override keyword is that it is stating to the compiler that you are attempting to override an inherited virtual function. If you made a mistake in matching the function signature of the function on the base class it won't compile as it must override an inherited function if declared as an override

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Point one was ensured by `Widget`, `Gadget` has no say in the matter, and certainly the appearance of the `virtual` keyword in `Gadget` makes no difference. – Ben Voigt Apr 20 '15 at 23:46