472,962 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

good code to return const reference to function local object?

Hello everyone,
1. Returning non-const reference to function local object is not
correct. But is it correct to return const reference to function local
object?

2. If in (1), it is correct to return const reference to function
local object, the process is a new temporary object is created (based
on the function local object) and the const reference is binded to the
temporary object, and the life time of the temporary object is
extended to the life time of const reference?

Or no need to create such a temporary object, just let the const
reference binded to the function local object itself?
thanks in advance,
George
Jan 23 '08 #1
3 5762
On Jan 23, 12:26 pm, George2 <george4acade...@yahoo.comwrote:
1. Returning non-const reference to function local object is not
correct. But is it correct to return const reference to function local
object?
No. What would that change?
2. If in (1), it is correct to return const reference to function
local object, the process is a new temporary object is created (based
on the function local object) and the const reference is binded to the
temporary object, and the life time of the temporary object is
extended to the life time of const reference?
No. To begin with, a temporary is never created when a
reference is initialized with an lvalue. And even if it were,
"A temporary bound to the [reference type] return value of a
function persists until the function exits".
Or no need to create such a temporary object, just let the
const reference binded to the function local object itself?
Const or not, the reference binds to the function local object,
which is destructed when the function exits.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 24 '08 #2
On 2008-01-24 12:48:23 -0500, va****@gmail.com said:
According to http://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378
.entry
You CAN return a const reference to a function local object and it IS
kept alive
as long as the reference lives.
You're confusing C++ with java.
>
[...]
--

-kira

Jan 24 '08 #3
On Jan 24, 6:48 pm, var...@gmail.com wrote:
According
tohttp://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378.entry
You CAN return a const reference to a function local object
and it IS kept alive as long as the reference lives.
According to the standard, you cannot. And I'd suggest you read
the article you site; there is no mention of any reference type
return values at all in it.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 25 '08 #4

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

Similar topics

10
by: florian kno | last post by:
hello all! i'm using gcc 3.2 on linux. i have a (self developed) matrix class: class cMatrix { // unneccessary info stripped .... double *data; // matrix data is stored in a heap array ...
2
by: Ryan Mitchley | last post by:
Hi all I have the functions friend CComplexMatrixTemp eye(const size_t nN); and friend CComplexMatrixTemp & chol(CComplexMatrixTemp &z); I would like create expressions of the form...
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...
7
by: pmatos | last post by:
Hi all, I've been having questions about strings, references, initializations... I've created code (which will not compile due to a reference problem) and I'd like comments on why this won't...
13
by: Matthias Kaeppler | last post by:
Hi, I was wondering why library implementors often make getter functions return strings by value (copies). For example, in boost::filesystem the leaf() function returns an std::string by value....
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
2
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
7
by: George2 | last post by:
Hello everyone, I am reading some code from other people, there are some code like this, class Foo { };
6
by: Jianwei Sun | last post by:
I am reading a peice of code which is at the following link: http://www.brpreiss.com/books/opus4/html/page141.html#SECTION007123000000000000000 The code is like this: Object&...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.