How to return reference to class object from functions?

W

Wang Tong

Suppose I have a function foo() and a class definition myClass, and I
want to create an instance of myClass and return it from foo(). I guess
the most efficient way is to return a reference to the object.

But here comes the problem: the object is a local one and will be
detroyed after function return. How can I return the reference from foo()?

Code:

myClass& foo()
{
myClass myObject;

...

return myObject; //Oops, myObject is a local one
}
 
D

David White

Wang Tong said:
Suppose I have a function foo() and a class definition myClass, and I
want to create an instance of myClass and return it from foo(). I guess
the most efficient way is to return a reference to the object.


But here comes the problem: the object is a local one and will be
detroyed after function return. How can I return the reference from foo()?

Code:

myClass& foo()
{
myClass myObject;

...

return myObject; //Oops, myObject is a local one

Right, so you can't do that.

If you examine your own words carefully, you should be able to deduce that
this is not an appropriate occasion for returning a reference. The reason
that returning a reference is efficient is that you aren't copying the local
object, but if you don't copy the local object it will no longer exist after
the function returns. In other words, the reason a reference is efficient is
also the reason you can't use one (in this case).

Of course there are other occasions - such as a class member function
returning a reference to a data member - in which the referred-to object is
not destroyed when the function returns. In those cases returning a
reference is okay.

DW
 
A

Agent Mulder

Wang Tong said:
Suppose I have a function foo() and a class definition myClass, and I
want to create an instance of myClass and return it from foo(). I guess
the most efficient way is to return a reference to the object.

But here comes the problem: the object is a local one and will be
detroyed after function return. How can I return the reference from foo()?

Code:

myClass& foo()
{
myClass myObject;

...

return myObject; //Oops, myObject is a local one
}



Code:

myClass& foo()
{
static myClass myObject;

...

return myObject; //myObject is static
}

-X
 
M

Michael Kochetkov

Wang Tong said:
Suppose I have a function foo() and a class definition myClass, and I
want to create an instance of myClass and return it from foo(). I guess
the most efficient way is to return a reference to the object.

But here comes the problem: the object is a local one and will be
detroyed after function return. How can I return the reference from foo()?

Code:

myClass& foo()
{
myClass myObject;

...

return myObject; //Oops, myObject is a local one
}
I believe it is much better to return by value in your case.
Static myObject may fail (it depends upon the internal logic of foo) in
expressions like this:
myClass aClass = foo() + foo();
Consider the case when foo modifies myObject. If you bring yourself to make
it static then it shall be properly documented.

In case of return by value a smart compiler would change the foo call:
myClass aClass(foo());
to something like this:
char __myClass_placeholder[sizeof(myClass)];
foo(__myClass_placeholder);
myClass& aClass = *reinterpret_cast<myClass*>(__myClass_placeholder);
...
~aClass();

If you believe you suffer performance penalties due to foo-like code then
you might wish to consider bridge pattern.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,373
Latest member
Desiree036

Latest Threads

Top