return reference or not

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Viet Le Hong

    return reference or not

    Should I return reference to a temporaty object in a function. I think I
    should but g++ 3.2 always warns this thing.

    Eg
    ..
    A& test(A &a, A &aa)
    {
    A res;
    .........
    .... doing some thing....

    return res;
    }


    Viet

  • Jakob Bieling

    #2
    Re: return reference or not

    "Viet Le Hong" <lhviet@iprimus .com.au> wrote in message
    news:3f6d6d1a$1 _1@news.iprimus .com.au...[color=blue]
    > Should I return reference to a temporaty object in a function. I think I
    > should but g++ 3.2 always warns this thing.[/color]

    Why do you think you should?
    [color=blue]
    > Eg
    > .
    > A& test(A &a, A &aa)
    > {
    > A res;
    > .........
    > .... doing some thing....
    >
    > return res;[/color]

    Here the object 'res' will be destroyed, so the reference is invalid.
    That is why it is not allowed. And yes, g++ 3.2 is right.
    [color=blue]
    > }[/color]

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)


    Comment

    • WW

      #3
      Re: return reference or not

      Jakob Bieling wrote:[color=blue]
      > "Viet Le Hong" <lhviet@iprimus .com.au> wrote in message
      > news:3f6d6d1a$1 _1@news.iprimus .com.au...[color=green]
      >> Should I return reference to a temporaty object in a function. I
      >> think I should but g++ 3.2 always warns this thing.[/color]
      >
      > Why do you think you should?
      >[color=green]
      >> Eg
      >> .
      >> A& test(A &a, A &aa)
      >> {
      >> A res;
      >> .........
      >> .... doing some thing....
      >>
      >> return res;[/color]
      >
      > Here the object 'res' will be destroyed, so the reference is
      > invalid. That is why it is not allowed. And yes, g++ 3.2 is right.[/color]

      Calrification: Actually it is allowed, "only" results in undefined behavior.
      g++ is very nice and warns about things like this but not all:

      char const *getC() {
      std::string x;
      // add something to x
      return x.c_str();
      }

      Same thing in another form, but g++ has already no way to detect this.

      --
      WW aka Attila


      Comment

      • WW

        #4
        Re: return reference or not

        WW wrote:[color=blue]
        > Calrification[/color]

        Khm. Clarification it is.

        --
        WW aka Attila


        Comment

        • jeffc

          #5
          Re: return reference or not


          "Viet Le Hong" <lhviet@iprimus .com.au> wrote in message
          news:3f6d6d1a$1 _1@news.iprimus .com.au...[color=blue]
          > Should I return reference to a temporaty object in a function. I think I
          > should ...[/color]

          What makes you think you "should"?


          Comment

          • Niklas Borson

            #6
            Re: return reference or not

            "jeffc" <nobody@nowhere .com> wrote in message news:<3f6f07f5_ 3@news1.prserv. net>...[color=blue]
            > "Viet Le Hong" <lhviet@iprimus .com.au> wrote in message
            > news:3f6d6d1a$1 _1@news.iprimus .com.au...[color=green]
            > > Should I return reference to a temporaty object in a function. I think I
            > > should ...[/color]
            >
            > What makes you think you "should"?[/color]

            Should a reference outlive the object it refers to?

            The answer to your question is the same as the answer to the
            above question.

            Comment

            Working...