Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to be warned when overriding a virtual method with wrong visibility

When overriding a virtual method, I noticed that when I make a mistake in the visibility (protected method overridden as a public method), I'm not warned by the compiler.

It is valid C++, but usually it is a mistake.

For example:

#include <iostream>

class Base
{
protected:
  virtual void ProtectedMethod(void)
  {
    std::cout << "Base::ProtectedMethod" << std::endl;
  }
};

class Derived : public Base
{
public:
  virtual void ProtectedMethod(void)
  {
    std::cout << "Derived::ProtectedMethod" << std::endl;
  }
};

int main(int, char* [])
{
  Derived d;
  d.ProtectedMethod();
}

I tried compiling with gcc and clang, with -Wall -Wextra, with no luck. I ran CppCheck on this code, still no luck.

What tool can help me detect this ? I need to fix the whole sources of a library I'm working on.

like image 355
Julien Avatar asked Sep 30 '11 12:09

Julien


1 Answers

Inspirel lets you define your own rules: http://www.inspirel.com/vera/

like image 164
Lou Franco Avatar answered Nov 15 '22 08:11

Lou Franco