Jonathan Jonathan - 6 months ago 119
C++ Question

Google Mock: Mocked overloaded functions create warning C4373

I'm mocking a C++ class which has 2 overloaded functions using Google Mock and VS2010:

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

#include "A.h"

class MockA : public A
// ...
MOCK_METHOD3(myFunc, void(const int id, const int errorCode, const CString errorMsg));
MOCK_METHOD1(myFunc, void(const CString errorMsg));
// ...

Each time I compile I get the following warning twice:

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'

Any idea why?

Is this correct behavior?

How can I avoid this?

Answer Source

If this is new code, you should be fine. The C4373 warning is saying that old versions of Visual Studio violated the standard. From the linked documentation:

Versions of the compiler prior to Visual C++ 2008 bind the function to the method in the base class, then issue a warning message. Subsequent versions of the compiler ignore the const or volatile qualifier, bind the function to the method in the derived class, then issue warning C4373. This latter behavior complies with the C++ standard.

This would only be a problem if you had broken code that relied on Visual Studio's incorrect behavior.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download