I had a frustrating problem recently that boiled down to a very simple coding mistake. Consider the following code:
#include <iostream>
class Base
{
public:
void func() { std::cout << "BASE" << std::endl; }
};
class Derived : public Base
{
public:
virtual void func() { std::cout << "DERIVED" << std::endl; }
};
int main(int argc, char* argv[])
{
Base* obj = new Derived;
obj->func();
delete obj;
return 0;
}
The output is
BASE
Obviously (for this case), I meant to put the virtual keyword on Base::func so that Derived::func would be called in main. I realize this is (probably) allowed by the c++ standard, and possibly with good reason, but it seems to me that 99% of the time this would be a coding mistake. However, when I compiled using g++ and all the -Wblah options I could think of, no warnings were generated.
Is there a way to generate a warning when both a base and derived class have member functions of the same name where the derived class's function is virtual and the base class's function is not?