By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

good code to return const reference to function local object?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.