如何避免Visual Studio下的名称隐藏警告

How to avoid name hiding warning under Visual Studio?

本文关键字:隐藏 警告 何避免 Visual Studio      更新时间:2023-10-16

有一个基类:

#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下,我得到一个脏编译。上面的TestSomething就是下面的Available

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下的警告来源是什么,如何清除它?

如果你查找错误C4266,你会发现它说A derived class did not override all overloads of a virtual function.,所以对于这个编译器,你需要覆盖unsigned int &变体可见的所有重载。

我没有在语言规范中查看这是否符合。