Google Mock:Mocked重载函数会创建警告C4373

Jon*_*han 9 c++ overloading mocking visual-c++ googlemock

我正在嘲笑一个使用Google Mock和VS2010的2个重载函数的C++类:

#include "stdafx.h"
#include "gmock/gmock.h"

#include "A.h"

class MockA : public A
{
public:
    // ...
    MOCK_METHOD3(myFunc, void(const int id, const int errorCode, const CString errorMsg));
    MOCK_METHOD1(myFunc, void(const CString errorMsg));
    // ...
};
Run Code Online (Sandbox Code Playgroud)

每次编译我都会收到两次以下警告:

1>c:\dev\my_project\tests\mocka.h(83): warning C4373: 'MockA::myFunc': virtual function overrides 'A::myFunc', previous versions of the compiler did not override when parameters only differed by const/volatile qualifiers
1>          c:\dev\my_project\my_project\include\a.h(107) : see declaration of 'A::myFunc'
Run Code Online (Sandbox Code Playgroud)

知道为什么吗?
这是正确的行为吗?
我怎么能避免这个?

Mat*_*hen 9

如果这是新代码,你应该没问题.该C4373警告是在说,Visual Studio中的旧版本违反标准.从链接的文档:

Visual C++ 2008之前的编译器版本将函数绑定到基类中的方法,然后发出警告消息.后续版本的编译器忽略const或volatile限定符,将函数绑定到派生类中的方法,然后发出警告C4373.后一种行为符合C++标准.

如果您破坏了依赖于Visual Studio不正确行为的代码,那么这只会是一个问题.


cod*_*ing 6

对我来说(在 VS 2010 中),指定const原始类型参数(我看到你也有)导致了这种行为。每当我想覆盖的基类函数中存在此类时,我都无法以某种方式指定模拟,以免发生此警告;当只有类类型 const value / const 引用参数时,警告从未发生。

所以在我看来,这种情况下的警告实际上是编译器中的一个错误(因为签名完全相同)。