MOCK_METHODn drops const qualifier on function parameter?

611 views
Skip to first unread message

Andrew Durward

unread,
May 8, 2009, 1:44:58 PM5/8/09
to Google C++ Mocking Framework
Hi,
First, thanks for the gmock library, great work.

I'm hoping somebody can help me with a little problem. If I try to
compile the following under MSVC 2005 SP1:

#include "gmock/gmock.h"

struct Some_interface
{
virtual void f( const int i ) = 0;
};

struct Some_mock : public Some_interface
{
MOCK_METHOD1( f, void( const int i ) );
};

I get the following error:

warning C4301: 'Some_mock::f': overriding virtual function only
differs from 'Some_interface::f' by const/volatile qualifier

It appears the const qualifier on the function parameter is being lost
somewhere. Am I missing something obvious here? I searched the
documentation and newsgroup archives but couldn't find any relevant
info.

Thanks for any help,
andrew

Zhanyong Wan (λx.x x)

unread,
May 8, 2009, 1:55:33 PM5/8/09
to Andrew Durward, Google C++ Mocking Framework
Andrew,

This is a VC bug. The same code compiles fine with gcc ,for example.

In C++, if you *declare* a function with a const parameter, the
'const' modifier is *ignored*. Therefore, your Some_interface is
equivalent to:

struct Some_interface {
virtual void f(int i) = 0; // int or const int? Makes no difference.
};

In fact, you can *declare* f() with an int parameter, and *define* it
with a 'const int' parameter. The compiler will still match them up.

Since 'const' is meaningless in the method *declaration*, I recommend
you to remove it in both Some_interface and Some_mock. That should
workaround the VC bug.

>
> Thanks for any help,
> andrew
>

--
Zhanyong

Zhanyong Wan (λx.x x)

unread,
May 8, 2009, 2:20:08 PM5/8/09
to Andrew Durward, Google C++ Mocking Framework
2009/5/8 Zhanyong Wan (λx.x x) <w...@google.com>:

I added an FAQ for this:

http://code.google.com/p/googlemock/wiki/FrequentlyAskedQuestions#MSVC_gives_me_warning_C4301_when_I_define_a_mock_method_with_a_c

>
>>
>> Thanks for any help,
>> andrew
>>
>
>
>
> --
> Zhanyong
>

--
Zhanyong

Andrew Durward

unread,
May 8, 2009, 2:37:21 PM5/8/09
to Google C++ Mocking Framework
Thanks for the quick response Zhanyong.

One more thing to clean up in the mess that is our legacy codebase...

andrew
> http://code.google.com/p/googlemock/wiki/FrequentlyAskedQuestions#MSV...
Reply all
Reply to author
Forward
0 new messages