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
{
public:
// ...
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?
For me (in VS 2010), specifying the const
on primitive type parameters (which I see you also have) caused this behavior. Whenever such existed in the base class function I wanted to override, I couldn't specify the mock in a way so that this warning did not occur; when only having class type const value / const reference parameters, the warning never occured.
So to me it seems like the warning in that case is actually a mistake in the compiler (as the signatures are exactly the same).
Suggested alternative approach:
#include "stdafx.h"
#include "gmock/gmock.h"
#include "A.h"
class MockA : public A
{
public:
// ...
void myFunc(const int id, const int errorCode, const CString errorMsg) {
mocked_myFunc3(id, errorCode, errorMsg);
}
void myFunc(const CString errorMsg) {
mocked_myFunc1(errorMsg);
}
MOCK_METHOD3(mocked_myFunc_3, void(const int id, const int errorCode, const CString errorMsg));
MOCK_METHOD1(mocked_myFunc_1, void(const CString errorMsg));
// ...
};
I realise this is an old question, but since I stumbled upon it myself now, I'd like to share my solution (or at least explanation):
The problem is likely that your declaration has a const parameter, which will be ignored by the compiler. It is the definition that may effectively use const for the parameter.
It's also mentioned now in the google mock faq now that, in order to get rid of the warning, remove const
from the parameter in the function declaration.
In my case I found it still hard since the function implementation was for a templated class inside a header where declaration and definition happen are both done together. Solution to that is probably to disable the warning when including the mocked class' header.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With