2012-02-02 81 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