Reference return methods and non-Reference return methods

1,924 views
Skip to first unread message

trycity

unread,
Aug 19, 2010, 3:59:29 PM8/19/10
to Google C++ Mocking Framework
Hello,

I am unable to figure out how to use the PrintTo method for non-
reference returns. For example, if I have a class

class Object;
class UseObject
{
public:
...
virtual Object& ( ) = 0;
}

the Mock class requires me to write:

void PrintTo(const Object&, std::ostream *){}

however what would I write if instead of Object&, I wanted to return
Object?

I would appreciate any help in this matter.

trycity

unread,
Aug 20, 2010, 8:19:03 AM8/20/10
to Google C++ Mocking Framework
I suppose I should clarify

If I have a virtual function such as

virtual Object& somefunc()=0;

then the Mock of this method is: MOCK_METHOD0(somefunc, Object&());

however, if I have a method which does not return a reference to an
object..

virtual Object somefunc()=0;

then I thought the Mock of this method was MOCK_METHOD0(somefunc,
Object());
This, however, results in compiler errors...how do I fix it?

Martin K. Svensson

unread,
Aug 20, 2010, 8:26:38 AM8/20/10
to trycity, Google C++ Mocking Framework

You do not say what errors you get. I don't think Google Mock should
have any problems with this. I am wildly guessing that you may get
errors because "Object" is only forward-declared or something.

--
/Martin

trycity

unread,
Aug 20, 2010, 8:30:07 AM8/20/10
to Google C++ Mocking Framework
Thanks for the reply,

Yes Object is only Forward declared and the errors disappear if use an
#include directive instead of the forward declaration...is there any
possible way to keep Object forward declared?

I will list the errors shortly.



On Aug 20, 8:26 am, "Martin K. Svensson"

Martin K. Svensson

unread,
Aug 20, 2010, 8:37:58 AM8/20/10
to trycity, Google C++ Mocking Framework
No need to list them, then:

In C/C++, you need a complete definition to copy an object to pass it as
a parameter by value.
A parameter by pointer or by reference does not need a completely
defined, because only the pointer/reference is copied.

You should keep using a const-reference.

/Martin

--
/Martin

trycity

unread,
Aug 20, 2010, 8:45:28 AM8/20/10
to Google C++ Mocking Framework
Thank you, I shall change the method to return by const-ref

On Aug 20, 8:37 am, "Martin K. Svensson"

Martin K. Svensson

unread,
Aug 20, 2010, 9:02:37 AM8/20/10
to trycity, Google C++ Mocking Framework
Argh, sorry, I mixed things up.

The _parameter_ should be const-reference, but the _return_ should be
by-value.
Then you need a complete definition after all.

I don't really know anything about PrintTo...

/Martin

--
/Martin

Reply all
Reply to author
Forward
0 new messages