473,395 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to return reference to class object from functions?

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
}

Jul 19 '05 #1
3 28103
Wang Tong <wa******@seas.upenn.edu> wrote in message
news:bo***********@netnews.upenn.edu...
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

Jul 19 '05 #2

"Wang Tong" <wa******@seas.upenn.edu> wrote in message news:bo***********@netnews.upenn.edu...
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
Jul 19 '05 #3

"Wang Tong" <wa******@seas.upenn.edu> wrote in message
news:bo***********@netnews.upenn.edu...
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.

--
Michael Kochetkov.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

94
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
19
by: Christian Engström | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
3
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I...
8
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
5
by: jmd | last post by:
hello. i am trying VC++ 7.1 (with vsnet 2003). my example : class Complex { public: Complex ( float re_ = 0.0, float im_ = 0.0 ) : re(re_), im(im_) {} float Re () { return ( re ); } float...
13
by: ctor | last post by:
Hi, I'm experiencing an annoying issue. Here is a simplified idea of what I am trying to do. Inclusion guards aren't shown for readability. I hope this isn't too confusing; I don't think a...
5
by: Tom | last post by:
If I have a container class that has a map member which stores pointers to objects that have been created via the new operator and I have a method that returns a entry in the map, would it be best...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.