简体   繁体   English

禁用某些方法的可视C ++虚拟函数覆盖警告

[英]Disabling visual C++ virtual function override warning for certain methods

I would like to enable C4263 (Visual C++) warning on our code base, however, the warning gives out some false positives. 我想在我们的代码库中启用C4263(Visual C ++)警告,但是,该警告给出了一些误报。 We would like to disable the warning such that only the false positives disappear. 我们希望禁用该警告,以便仅误报消失。 I tried to simplify the issue with some generic code: 我试图用一些通用代码简化问题:

class B {
public:
    virtual void funcA();
    virtual void funcB();
};


class D : public B
{
    virtual void funcA(int);    
    virtual void funcB(int);
};

With this code we I get following warnings: void D::funcA(int)' : member function does not override any base class virtual member function void D::funcB(int)' : member function does not override any base class virtual member function 通过此代码,我们得到以下警告:void D :: funcA(int)':成员函数不会覆盖任何基类虚拟成员函数void D :: funcB(int)':成员函数不会覆盖任何基类虚拟成员功能

What I am trying to achieve is to disable the warning for funcA (or funcB) and let the rest of the class be affected by it. 我想要实现的是禁用funcA(或funcB)警告,并让该类的其余部分受到它的影响。

I tried putting 我试过

#pragma warning(push)        
#pragma warning(disable:4263)
 .
 .
 .
#pragma warning(pop)    

around funcA but that does not solve the problem. 围绕funcA,但这不能解决问题。 If the pragmas wrap the whole class that both of the warnings disappear. 如果实用程序包裹了整个班级,则所有警告均会消失。

Any ideas? 有任何想法吗?

This is a strange error given the documentation for it: 给定其文档,这是一个奇怪的错误:

https://msdn.microsoft.com/en-us/library/ay4h0tc9.aspx?f=255&MSPPError=-2147217396 https://msdn.microsoft.com/zh-cn/library/ay4h0tc9.aspx?f=255&MSPPError=-2147217396

'function' : member function does not override any base class virtual member function 'function':成员函数不会覆盖任何基类虚拟成员函数

A class function definition has the same name as a virtual function in a base class but not the same number or type of arguments. 类函数定义与基类中的虚函数具有相同的名称,但参数的数量或类型不同。 This effectively hides the virtual function in the base class. 这有效地将虚拟函数隐藏在基类中。

The last sentence is the interesting one, and it suggested to me that if you unhide the base class functions, the warning will no longer appear. 最后一句话很有趣,它向我建议,如果取消隐藏基类函数,该警告将不再出现。

And indeed this is the case. 的确如此。 The following code does not output C4263: 以下代码不输出C4263:

class B {
public:
    virtual void funcA();
    virtual void funcB();
};

class D : public B
{
    using B::funcA;
    using B::funcB;
    virtual void funcA(int);    
    virtual void funcB(int);
};

The warning seems a little strange. 该警告似乎有些奇怪。 If you're dispatching from the base class pointer, it doesn't matter what functions the derived class hides, as they won't be hidden when using a base class pointer. 如果从基类指针调度,则派生类隐藏什么功能都没有关系,因为使用基类指针时它们不会被隐藏。 But herein lies the answer! 但答案就在这里!

What's actually happening here is the compiler is guessing your intentions. 实际上,编译器正在猜测您的意图。 Because you are introducing a new signature, it means you will be using either polymorphic or non-polymorphic dispatch using a derived pointer (not a base pointer). 因为要引入新的签名,所以这意味着您将使用派生指针(而不是基本指针)使用多态调度或非多态调度。 If you were not doing this, it would be impossible to call your overload. 如果您不这样做,就不可能调用您的重载。 The compiler figures that if you are doing this, you will be hiding the un-overridden functions. 编译器认为,如果执行此操作,则将隐藏未覆盖的函数。 And that's what the warning is about. 这就是警告的内容。

In an example: 在一个示例中:

struct Base
{
    virtual void DoThing(int)
    {
        std::cout << "INT  " << std::endl;
    }
};

struct Derived: public Base
{
    virtual void DoThing(char) // Add a function to handle chars
    {
        std::cout << "CHAR  " << std::endl;
    }
};

int main()
{
    Derived *derived = new Derived;
    Base *base = derived;

    base->DoThing(1);
    derived->DoThing(1);
    derived->DoThing('a');
}

This outputs: 输出:

INT CHAR CHAR INT CHAR CHAR

The intention may have been to add an overload to handle a different case, but instead it's hiding all the existing overloads. 目的可能是添加一个重载来处理不同的情况,但是它隐藏了所有现有的重载。 The warning is correct given the rules of the language. 根据语言规则,警告是正确的。 The warning isn't exact, it's trivial to determine cases where it doesn't get invoked but it should. 该警告并不准确,要确定没有被调用但应该被调用的情况是微不足道的。 It in-fact does the opposite of false-warnings :) 实际上,它与错误警告相反:)

To defeat this warning, the using declaration should be used. 为了消除此警告,应使用using声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM