good code to return const reference to function local object?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George2

    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
  • James Kanze

    #2
    Re: good code to return const reference to function local object?

    On Jan 23, 12:26 pm, George2 <george4acade.. .@yahoo.comwrot e:
    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:james.kan ze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientier ter Datenverarbeitu ng
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

    Comment

    • Kira Yamato

      #3
      Re: good code to return const reference to function local object?

      On 2008-01-24 12:48:23 -0500, varung@gmail.co m said:
      According to http://herbsutter.spac es.live.com/blog/cns!2D4327CC297 151BB!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

      Comment

      • James Kanze

        #4
        Re: good code to return const reference to function local object?

        On Jan 24, 6:48 pm, var...@gmail.co m wrote:
        According
        tohttp://herbsutter.spac es.live.com/blog/cns!2D4327CC297 151BB!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:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        Working...