Reputation: 16832
How do I make GCC inform me about redefining member functions with a different signature?
I am porting a large project to Android.
Due to the differences in the M$VC and GCC dialects, I inserted a number of const
modifiers for function parameters.
(Namely, when you call func(MyClass(something))
, M$ is ok with MyClass&
while GCC requires const MyClass&
.)
The gotcha is that when you change the signature of BaseClass::func()
, you get no warning about having a different signature for DerivedClass::func()
.
Here is a small program showing what's going on:
#include <stdio.h>
class Value {
int x;
public:
Value(int xx):x(xx){}
};
class Base {
public:
virtual void f(const Value&v) {printf("Base\n");}
};
class First: public Base {
public:
virtual void f(Value&v) {printf("First\n");}
};
class Second: public Base {
public:
virtual void f(Value&v) {printf("Second\n");}
};
int main() {
Value v(1);
First first;
Second second;
Base*any;
any = &first;
any->f(v);
any->f(Value(2));
first.f(v);
//first.f(Value(3)); // error: no matching function for call to
First::f(Value)
}
When I compile and run it, I get:
$ g++ -Wall -Wextra inher2.cpp
inher2.cpp:10:18: warning: unused parameter ‘v’
inher2.cpp:15:18: warning: unused parameter ‘v’
inher2.cpp:20:18: warning: unused parameter ‘v’
$ ./a.out
Base
Base
First
$
(gcc is right about the unused parameter, but it's irrelevant.)
So: How do I make GCC inform me about redefining member functions with a different signature?
Upvotes: 2
Views: 179
Reputation: 1801
That should be -Woverloaded-virtual.
-Woverloaded-virtual
Warn when a derived class function declaration may be an error in defining a virtual function (C++ only). In a derived class, the definitions of virtual functions must match the type signature of a virtual function declared in the base class. With this option, the compiler warns when you define a function with the same name as a virtual function, but with a type signature that does not match any declarations from the base class.
Upvotes: 4