C++11 added override
to ensure that member functions you write that you intend to override base-class virtual functions actually do (or won't compile).
But in a large object hierarchy, sometimes you could accidentally end up writing a member function that overrides a base-class virtual when you didn't intend it! For instance:
struct A {
virtual void foo() { } // because obviously every class has foo().
};
struct B : A { ... };
class C : B {
private:
void foo() {
// was intended to be a private function local to C
// not intended to override A::foo(), but now does
}
};
Is there some compiler flag/extension that would at least issue a warning on C::foo
? For readability and correctness, I just would like to enforce that all overrides use override
.
GazimpleWidget(Widget& w)
and obviouslyC::GazimpleWidget(Widget& w)
still gazimples widgets. You only get such problems when you try to abbreviateC::GazimpleWidget( )
toC::GW( )
. Don't do that. – Protaminefoo
is misleading. Real functions with real names are far less likely to collide unintentionally. If they are named the same, they should be doing the same and then overriding is probably not an error. – Protamine