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)?