Virtual/non-virtual mixups

Tuesday 24 February 2004

The Microsoft Visual C++ compiler will warn you about all sorts of harmless things. Today I ran across a harmful thing it would not warn me about, and I am really surprised.

The bug was a method declared non-virtual in a base class, then virtual in a derived class:

class CBase
    void DoIt();

class CDerived: public CBase
    virtual void DoIt();

An instance of CDerived was being used via a CBase pointer, so the CDerived::DoSomething() method wasn’t being called:

CBase * pBase = new CDerived();
pBase->DoIt();  // Calls CBase::DoIt, not CDerived::DoIt.

I tried my hardest to get the compiler to generate a warning about the mismatch, but could not. It’s hard to imagine a coder wanting to produce this situation (a virtual hiding a non-virtual), and there are warnings for tons of situations that are perfectly harmless. I’m amazed that there’s no warning for this.

I even tried /Wall, which means show absolutely all the warnings. For one .cpp file, this produced over 3800 warnings, most of which were about STL functions that were not inlined. No mention of my hidden virtual function. D’oh!


Chris 6:22 AM on 26 Feb 2004

Am certainly not a developper... so this maybe a very silly question... but this raised the question in my mind as how you would derive a class where you would want to "virtualise" an inherited method so that subsequent derived class could do as they are pleased.

Ned Batchelder 6:39 AM on 26 Feb 2004

I'm guessing there's no warning for this construct precisely because a developer may want to inherit from a base class, then make the method virtual, so that the tree of classes below the derived class gets the "virtuality".

Still seems to me like more often a problem than a solution, and it should have a warning.

Bob 9:37 AM on 26 Feb 2004

Potential problems like this are one reason that there are tools such as Parasoft CodeWizard. One advantage of such tools is that you can define your own rules for potential coding problems that go above and beyond the base language. I wonder if there are any open source tools like this for C++?

We've used JTest, PMD and FindBugs on our Java code and they've all been very useful for finding issues. The latter two are open source tools.

Sylvain Galineau 11:24 AM on 3 Mar 2004

I can see why. As far as both the C++ spec the compiler are concerned, these methods to do not conflict in any way. In CDerived, the virtual qualifier essentially gives the method a different type; and under the covers, it is also true in most implementations : one is accessed through the vtable, the other isn't. It's as if they were in different namespaces. However, I agree the construct is more likely to be harmful than intentional. One would expect a built-in way to flag this. Odd. I wonder what other platforms/compilers do.

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.