2012-02-02 79 views
2

如何让GCC通知我使用不同的签名重新定义成员函数?需要关于用const参数覆盖函数的警告

我正在将一个大型项目移植到Android。 由于M $ VC和GCC方言的不同,我为函数参数插入了一些const修饰符。 (即,当你调用func(MyClass(something)),M $是确定与MyClass&而GCC需要const MyClass&。)

的小问题,就是当你改变的BaseClass::func()签名,你不会看到任何警告有关具有DerivedClass::func()不同的签名。

下面是一个小程序,显示这是怎么回事:

#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) 
} 

当我编译并运行它,我得到:

$ 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是正确的关于未使用的参数,但它是不相关)

所以: 我该如何让GCC通知我用不同的签名重新定义成员函数?

回答

4

这应该是-Woverloaded-虚拟。

-Woverloaded虚拟

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. 
+0

这总比没有好_much_,但无论是不是一个完美的解决方案。当我真正感兴趣的只是那些不同于''const''修饰符的函数时,我得到像void func(SomeType&)这样的消息_lots_被SomeType func()隐藏。 – 18446744073709551615 2012-02-03 07:17:18

+0

@ 18446744073709551615然后我忍不住。这些都是值得关注的事情。为什么你会得到那么多隐藏的功能 - 蹩脚的命名约定? – visitor 2012-02-03 10:32:26

+0

_因为代码是在_decades_前编写的,而不是由我编写的。有些文件可以追溯到1994年。 – 18446744073709551615 2012-02-09 11:29:14