Assign return value to reference

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

    #1

    Assign return value to reference

    Hi,

    I'm trying to figure out if the following code is correct or could cause problems in some way...

    class MyClass {
    ...
    };

    MyClass doSomething() {
    ...
    MyClass obj;
    ....
    return obj;
    }

    int main() {
    MyClass &ret = doSomething();
    ...
    return 0;
    }

    So function doSomething() returns an object, and "ret" is referring to that object. The question is now whether "ret" refers to a valid object or not?

    --
    Knut Stolze
    Data Warehousing for DB2 z/OS
    IBM Germany Research & Development
  • anon

    #2
    Re: Assign return value to reference

    Knut Stolze wrote:
    Hi,
    >
    I'm trying to figure out if the following code is correct or could cause problems in some way...
    >
    class MyClass {
    ...
    };
    >
    MyClass doSomething() {
    ...
    MyClass obj;
    ....
    return obj;
    }
    >
    int main() {
    MyClass &ret = doSomething();
    ...
    return 0;
    }
    >
    So function doSomething() returns an object, and "ret" is referring to that object. The question is now whether "ret" refers to a valid object or not?
    >
    Your example doesnt compile. However, I tried to compile this:
    class MyClass {
    };
    MyClass doSomething() {
    MyClass obj;
    return obj;
    }
    int main() {
    MyClass &ret = doSomething();
    return 0;
    }

    with gcc without any additional options, and I got :
    o1.cpp: In function ‘int main()’:
    o1.cpp:10: error: invalid initialization of non-const reference of type
    ‘MyClass&’ from a temporary of type ‘MyClass’


    btw don't forget to take a look at this:

    Comment

    • Leandro Melo

      #3
      Re: Assign return value to reference

      On 19 ago, 09:13, Knut Stolze <sto...@de.ibm. comwrote:
      Hi,
      >
      I'm trying to figure out if the following code is correct or could cause problems in some way...
      >
      class MyClass {
          ...
      >
      };
      >
      MyClass doSomething() {
          ...
          MyClass obj;
          ....
          return obj;
      >
      }
      >
      int main() {
          MyClass &ret = doSomething();
          ...
          return 0;
      >
      }
      >
      So function doSomething() returns an object, and "ret" is referring to that object.  The question is now whether "ret" refers to a valid object ornot?
      Hi.

      It does not. In fact, it should give you a compiler error (or at least
      a warning) because the return of doSomething is temporary. Using a
      const-ref in this case is allowed because the life-time of the copy
      will be extended.

      int main()
      {
      const MyClass &ret = doSomething(); //Notice the const.
      ...
      return 0;
      }

      --
      Leandro T. C. Melo

      Comment

      • Knut Stolze

        #4
        Re: Assign return value to reference

        Leandro Melo wrote:
        On 19 ago, 09:13, Knut Stolze <sto...@de.ibm. comwrote:
        >Hi,
        >>
        >I'm trying to figure out if the following code is correct or could cause
        >problems in some way...
        >>
        >class MyClass {
        >...
        >>
        >};
        >>
        >MyClass doSomething() {
        >...
        >MyClass obj;
        >....
        >return obj;
        >>
        >}
        >>
        >int main() {
        >MyClass &ret = doSomething();
        >...
        >return 0;
        >>
        >}
        >>
        >So function doSomething() returns an object, and "ret" is referring to
        >that object. The question is now whether "ret" refers to a valid object
        >or not?
        >
        Hi.
        >
        It does not. In fact, it should give you a compiler error (or at least
        a warning) because the return of doSomething is temporary. Using a
        const-ref in this case is allowed because the life-time of the copy
        will be extended.
        >
        int main()
        {
        const MyClass &ret = doSomething(); //Notice the const.
        ...
        return 0;
        }
        Sorry, my fault. I should have written a short program to demonstrate the issue.

        Where can I find the details (in the C++ standard) on the life-time extension of the temporary copy?

        --
        Knut Stolze
        Data Warehousing for DB2 z/OS
        IBM Germany Research & Development

        Comment

        • Juha Nieminen

          #5
          Re: Assign return value to reference

          Leandro Melo wrote:
          const MyClass &ret = doSomething(); //Notice the const.
          AFAIK, in practice there's absolutely no difference between that line
          and this one:

          const MyClass ret = doSomething();

          I could even guess most compilers will probably generate the exact
          same machine code from both (although some compilers might not, ie. they
          might allocate the object at different parts depending on the situation,
          but the effect will nevertheless be exactly the same).

          Comment

          • Leandro Melo

            #6
            Re: Assign return value to reference

            On 19 ago, 12:17, Juha Nieminen <nos...@thanks. invalidwrote:
            Leandro Melo wrote:
                 const MyClass &ret = doSomething(); //Notice the const.
            >
              AFAIK, in practice there's absolutely no difference between that line
            and this one:
            >
                 const MyClass ret = doSomething();
            >
              I could even guess most compilers will probably generate the exact
            same machine code from both (although some compilers might not, ie. they
            might allocate the object at different parts depending on the situation,
            but the effect will nevertheless be exactly the same).

            So could I ;) For this particular case it will probably not make a
            difference from a practical point of view. But I wanted to show that
            const refs can be used to extend the life-time of temporary objects,
            which can be useful in other situations.

            --
            Leandro T. C. Melo

            Comment

            • Thomas J. Gritzan

              #7
              Re: Assign return value to reference

              Juha Nieminen schrieb:
              Leandro Melo wrote:
              > const MyClass &ret = doSomething(); //Notice the const.
              >
              AFAIK, in practice there's absolutely no difference between that line
              and this one:
              >
              const MyClass ret = doSomething();
              >
              I could even guess most compilers will probably generate the exact
              same machine code from both (although some compilers might not, ie. they
              might allocate the object at different parts depending on the situation,
              but the effect will nevertheless be exactly the same).
              It might generate the same machine code in this case, but if you don't
              use a reference and you have a different type for the local variable
              than the return type, the return value gets sliced.

              Example:

              class Base {};
              class MyClass : public Base {};
              MyClass doSomething() {
              return MyClass();
              }
              int main() {
              const Base base1 = doSomething();
              const Base &base2 = doSomething();
              return 0;
              }

              base1 actually *is* a Base, base2 references an object of type MyClass.

              --
              Thomas

              Comment

              Working...