1

Quite often I see code (e.g. here) where the override is also declared virtual even if the class is not meant to be subclassed again. E.g.

class A {
    virtual void foo();
}
class B : public A {
    virtual void foo();
}
// there is no subclass of B (and most likely there will never be one)

In my naive C++ beginners thinking, I find it much clearer and more explicit not to declare B::foo virtual in case it is not meant to be overridden. I.e.

class B : public A {
     void foo();
}

Many methods I write are not meant to be overridden. On the other hand I understand virtual as indication that the method was designed to be overridden. I guess it is just a matter of taste, however I am not 100% sure:

Does it make any difference if B::foo() is declared virtual (assuming B will not be used as base class)?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    No, `foo` will automatically be `virtual` in `B` since it was in `A`. It doesn't hurt to leave it there though. Especially if someone wants to derive from `B`, then they know `foo` is `virtual` without having to walk up the inheritance. – Cory Kramer Jun 01 '15 at 11:33
  • 2
    Putting an `override` in will actually force the compiler to check it at least. – chris Jun 01 '15 at 11:34
  • 1
    As @CoryKramer said, it's not needed, but it also doesn't hurt and is good for readability and documentation for others. – Some programmer dude Jun 01 '15 at 11:35
  • @CoryKramer so it wouldnt make a difference for a subclass whether I explcitly put `virtual` in `B` or not? If this is true, maybe I should change my habit and always put the `virtual` also in the subclass.... – 463035818_is_not_an_ai Jun 01 '15 at 11:35
  • @juanchopanza Indeed it is a duplicate. However, I think I managed quite well to ask for "good style" and getting good answers without making the question too broad or opinion based. I think this aspect is not covered in the duplicate. – 463035818_is_not_an_ai Jun 01 '15 at 12:38

3 Answers3

3

No, it doesn't make any difference. If A::foo is declared as virtual in the base class, B::foo is also virtual: whether you declare it as such or not.

Shoe
  • 74,840
  • 36
  • 166
  • 272
3

There is no requirement to mark an overriden virtual method virtual in a derived class. The decision whether a method is virtual or not is made in the least derived class.

It is, however, a good idea to mark a virtual method explicitly virtual in a derived class for two reasons:

  1. It serves as documentation for future readers of your code. That could be you, so this should be a strong incentive.
  2. If someone decides to remove the virtual qualifier in the base class, a compiler can catch the bug before the application ships, and issue a warning. There is no requirement for a particular implementation to issue a diagnostic in this case, though.


If your compiler supports C++11, using the override specifier in derived classes is recommended, as it performs additional validation. It is required for an implementation to issue an error, if a base class method is not declared virtual, but the derived class uses an override specifier.
IInspectable
  • 46,945
  • 8
  • 85
  • 181
2

A method doesn't lose is virtual qualifier, so if A::foo() is virtual, B::foo() is also virtual even without repeating the keyword in B::foo() declaration.

Before override, it may be a style to repeat virtual in derived classes to remember that it is a overridden method, now we can use override instead (and compiler check that it is correct whereas it can't do it simply with virtual)

Many methods I write are not meant to be overridden

So you may mark them as final to forbid the method to be overridden (you may also mark the class final if it makes sense).

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • I know `final` from java and I always liked it that it is much easier in java to express that a class is not meant to be overridden. Unfortunately I have to work "pre C++11" – 463035818_is_not_an_ai Jun 01 '15 at 12:32