How to return reference to class object from functions?

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

    How to return reference to class object from functions?

    Suppose I have a function foo() and a class definition myClass, and I
    want to create an instance of myClass and return it from foo(). I guess
    the most efficient way is to return a reference to the object.

    But here comes the problem: the object is a local one and will be
    detroyed after function return. How can I return the reference from foo()?

    Code:

    myClass& foo()
    {
    myClass myObject;

    ...

    return myObject; //Oops, myObject is a local one
    }

  • David White

    #2
    Re: How to return reference to class object from functions?

    Wang Tong <wangtong@seas. upenn.edu> wrote in message
    news:bo9rue$7ua l$1@netnews.upe nn.edu...[color=blue]
    > Suppose I have a function foo() and a class definition myClass, and I
    > want to create an instance of myClass and return it from foo(). I guess
    > the most efficient way is to return a reference to the object.
    >
    >
    > But here comes the problem: the object is a local one and will be
    > detroyed after function return. How can I return the reference from foo()?
    >
    > Code:
    >
    > myClass& foo()
    > {
    > myClass myObject;
    >
    > ...
    >
    > return myObject; //Oops, myObject is a local one[/color]

    Right, so you can't do that.
    [color=blue]
    > }[/color]

    If you examine your own words carefully, you should be able to deduce that
    this is not an appropriate occasion for returning a reference. The reason
    that returning a reference is efficient is that you aren't copying the local
    object, but if you don't copy the local object it will no longer exist after
    the function returns. In other words, the reason a reference is efficient is
    also the reason you can't use one (in this case).

    Of course there are other occasions - such as a class member function
    returning a reference to a data member - in which the referred-to object is
    not destroyed when the function returns. In those cases returning a
    reference is okay.

    DW



    Comment

    • Agent Mulder

      #3
      Re: How to return reference to class object from functions?


      "Wang Tong" <wangtong@seas. upenn.edu> wrote in message news:bo9rue$7ua l$1@netnews.upe nn.edu...[color=blue]
      > Suppose I have a function foo() and a class definition myClass, and I
      > want to create an instance of myClass and return it from foo(). I guess
      > the most efficient way is to return a reference to the object.
      >
      > But here comes the problem: the object is a local one and will be
      > detroyed after function return. How can I return the reference from foo()?
      >
      > Code:
      >
      > myClass& foo()
      > {
      > myClass myObject;
      >
      > ...
      >
      > return myObject; //Oops, myObject is a local one
      > }[/color]



      Code:

      myClass& foo()
      {
      static myClass myObject;

      ...

      return myObject; //myObject is static
      }

      -X


      Comment

      • Michael Kochetkov

        #4
        Re: How to return reference to class object from functions?


        "Wang Tong" <wangtong@seas. upenn.edu> wrote in message
        news:bo9rue$7ua l$1@netnews.upe nn.edu...[color=blue]
        > Suppose I have a function foo() and a class definition myClass, and I
        > want to create an instance of myClass and return it from foo(). I guess
        > the most efficient way is to return a reference to the object.
        >
        > But here comes the problem: the object is a local one and will be
        > detroyed after function return. How can I return the reference from foo()?
        >
        > Code:
        >
        > myClass& foo()
        > {
        > myClass myObject;
        >
        > ...
        >
        > return myObject; //Oops, myObject is a local one
        > }[/color]
        I believe it is much better to return by value in your case.
        Static myObject may fail (it depends upon the internal logic of foo) in
        expressions like this:
        myClass aClass = foo() + foo();
        Consider the case when foo modifies myObject. If you bring yourself to make
        it static then it shall be properly documented.

        In case of return by value a smart compiler would change the foo call:
        myClass aClass(foo());
        to something like this:
        char __myClass_place holder[sizeof(myClass)];
        foo(__myClass_p laceholder);
        myClass& aClass = *reinterpret_ca st<myClass*>(__ myClass_placeho lder);
        ...
        ~aClass();

        If you believe you suffer performance penalties due to foo-like code then
        you might wish to consider bridge pattern.

        --
        Michael Kochetkov.


        Comment

        Working...