Tuesday 24 February 2004 — This is over 20 years old. Be careful.
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
{
public:
void DoIt();
};
class CDerived: public CBase
{
public:
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!
Comments
Still seems to me like more often a problem than a solution, and it should have a warning.
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.
Add a comment: