468,469 Members | 1,892 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,469 developers. It's quick & easy.

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 5549
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by florian kno | last post: by
2 posts views Thread by Ryan Mitchley | last post: by
7 posts views Thread by pmatos | last post: by
13 posts views Thread by Matthias Kaeppler | last post: by
reply views Thread by Daniel Sélen Secches | last post: by
2 posts views Thread by Eric Lilja | last post: by
7 posts views Thread by George2 | last post: by
6 posts views Thread by Jianwei Sun | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.