I just stumbled over a use case of the override specifier that, as far as I can tell, seems redundant and also without any particular semantics meaning,
In case of pure virtual functions and compilations not really. You will get an error anyway (except in as in the example from Pete)
But the error message may be more readable if you get an error like "your function does not override anything" compared to later "cannot instantiate abstract class"
Another advantage would be while reading the declaration you know this is a derived method from a base class.
Also it is good practice to just get used to declare all overridden methods with override
. So why make a difference here and have inconsistent style.
As to why it is good to have all overriden methods be declared override
:
imagine you have
class A
{
virtual void Foo();
};
class B: public A
{
virtual void Foo() override;
};
And then you change Foo
to a const
function in A
. Without override
this will compile but when you call A->foo()
and it is a B object B->foo()
will not be called without any indication something changed there. With override
you get an error here.