Mark
Mark

Reputation: 4109

Are there any subtleties in using both the virtual and override keywords in C++11?

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 {}
}

Upvotes: 24

Views: 2169

Answers (3)

mindriot
mindriot

Reputation: 5668

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."

Upvotes: 4

Ben Voigt
Ben Voigt

Reputation: 283684

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                 |

Upvotes: 35

Cory Kramer
Cory Kramer

Reputation: 117886

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

Upvotes: 1

Related Questions