2015-10-17 80 views
0

我有一個基類:如何避免在Visual Studio下的名稱隱藏警告?

#define OUT 
#define NO_VTABLE __declspec(novtable) 

class NO_VTABLE Foo 
{ 
public: 

    virtual bool TestSomething() const = 0; 

    virtual bool TestSomething(OUT unsigned int& extendedInfo) const { 
     UNUSED(extendedInfo); 
     return TestSomething(); 
    } 
}; 

而派生類:

class NO_VTABLE Bar : public Foo 
{ 
public: 

    virtual bool TestSomething() const { 
     // Do the test, return the result... 
    } 
}; 

GCC下,該程序與-Wall -Woverloaded-virtual完全編譯。在Visual Studio下,我得到一個髒編譯。以上顯示的TestSomethingAvailable

1> ...\derived.h(78) : warning C4266: 'bool DeviceState::Available(unsigned int &) const' : 
    no override available for virtual member function from base 'DeviceState'; function is hidden 
1>  ...\base.h(794) : see declaration of 'DeviceState::Available' 
1>  ...\base.h(787) : see declaration of 'DeviceState' 

刪除NO_VTABLE沒有區別。警告仍然存在。

所有TestSomething都是公開的在基類和派生類中都是虛擬的,所以它對我來說不清楚從調用者隱藏什麼。

我正在Visual Studio下進行測試,並且在Visual Studio 2005,2008和2010上都遇到過它。我還有其他VS進行測試,但在這一點上,我知道它不是一次性的,關閉。

我不想關閉警告,因爲文件base.h很大,有很多類,它可能在將來遇到其他問題。

Visual Studio聲明對調用者隱藏了什麼? Visual Studio下警告的來源是什麼?如何清除它?

+0

作爲一種解決方法,您可以在導致問題的行周圍推送和彈出警告 - 畢竟這些都是非常舊的編譯器。 – Rostislav

+0

@Rostislav - 謝謝。編譯器版本與它有什麼關係?據我所知,都是C++ 03,它符合項目的要求。 – jww

回答

1

如果你查找錯誤C4266,你會發現它說A derived class did not override all overloads of a virtual function.因此,對於這個編譯器,你需要覆蓋unsigned int &變體的所有重載。

我沒有擡頭看語言規範,看看這是否符合。

+0

謝謝。對我來說混亂的一點是微軟聲稱它是隱藏的。如何/爲什麼隱藏? – jww

+0

@jww由於'Testsomething()'在Bar中被重寫,但是'TestSomething(unsigned int&)'不是。在類中有一個重載覆蓋隱藏了非虛函數的基類中的所有其他重載,並且Microsoft也將其應用於虛函數。也可能要指出的是,在層次結構的某些部分('Bar'),重載將根本不可見,並且需要一些額外的投射來達到它。 – 1201ProgramAlarm

+0

它被隱藏了。你是否應該關心這個問題還不清楚。我們對你的用例或你最派生類的外觀一無所知。 –