Warning for Missing Virtual Keyword
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 开发者_Python百科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?
In Visual C++ you can use the override
extension. Like this:
virtual void func() override { std::cout << "DERIVED" << std::endl; }
This will give an error if the function doesn't actually override a base class method. I use this for ALL virtual functions. Typically I define a macro like this:
#ifdef _MSC_VER
#define OVERRIDE override
#else
#define OVERRIDE
#endif
So I can use it like this:
virtual void func() OVERRIDE { std::cout << "DERIVED" << std::endl; }
I've looked for something like this in g++ but couldn't find a similar concept.
The only thing I dislike about it in Visual C++ is that you can't have the compiler require it (or at least warn) on all overridden functions.
I don't know of any g++ flag to produce a warning on this (not to say there isn't one), but I'd say this is a pretty rare error. Most people write the base class first, as an interface using pure virtual functions. If you had said:
void func() = 0;
then you would get a syntax error.
man gcc
-Woverloaded-virtual (C++ and Objective-C++ only) Warn when a function declaration hides virtual functions from a base class. For example, in:
struct A {
virtual void f();
};
struct B: public A {
void f(int);
};
the "A" class version of "f" is hidden in "B", and code like:
B* b;
b->f();
will fail to compile.
精彩评论