English 中文(简体)
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
{
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:devmy_project	estsmocka.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:devmy_projectmy_projectincludea.h(107) : see declaration of  A::myFunc 

Any idea why?
Is this correct behavior?
How can I avoid this?

最佳回答

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.

问题回答

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签