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.
Inspirel lets you define your own rules: http://www.inspirel.com/vera/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With