Re: Function Returning a local object???

From: Dave Townsend (datownsend_at_comcast.net)
Date: 09/06/04

  • Next message: E. Robert Tisdale: "Re: Function Returning a local object???"
    Date: Sun, 5 Sep 2004 17:55:25 -0700
    
    

    "Olumide" <50295@web.de> wrote in message
    news:c837e7e8.0409051616.2d642d3e@posting.google.com...
    > I'm studying Nigel Chapman's Late Night Guide to C++ which I think is
    > an absolutely fantastic book; however on page 175 (topic: operator
    > overlaoding), there the following code snippet:
    >
    > inline MFVec operator+(const MFVec& z1, const MFVec& z2) // Global
    > function
    > {
    > MFVec res = z1;
    > res += z2
    > return res; // WHY???
    > }
    >
    > MFVec is the (vector) class:
    >
    > class MFVec {
    > public:
    > MFVec(float x, float y);
    > // other member functions
    > private:
    > float xcoord, ycoord;
    > }
    >
    > My problem however lies with the line 3 of the global operator+()
    > function because it returns the local variable res, which I assume is
    > destroyed as soon as the function is exited. This resembles a dangling
    > refrence and I don't know why its not illegal. Anyone knows why?
    >
    > I don't mind thorough answers; in fact I kidda prefer them ;-)
    >
    > Thanks

    No, this code is righteous, its ok to return an object, although the object
    "res"will
    be destroyed it will be copied and the copy returned to you ( but
    compilers
    will generally optimize this sequence and return the actual res to you and
    not destroy it).
    The example you quote is idiomatic in the sense that it is the pattern used
    to
    implement classes which define the + operation such as complex numbers,
    vectors, etc.
    You can check this by looking at the "this" pointer for the "res" object and
    the returned
    object, they should be different ( or at least in debug compile mode).

    You might be confusing returning references to local objects. This is pure
    evil...
    once the function returns the value, the object is destroyed and you are
    hanging onto
    a invalid reference. For instance :

    foo& getfoo(){ foo f; return f;}
    ...
    foo& g = getfoo(); // f is not a valid object anymore now so g is not a
    valid reference...

    You can put in some debug printfs in the constructors, destructors, and you
    should
    see that the reference you are getting back is already toasted.

    Sometimes you will see member functions of a class returning
    references to objects, this is ok when the objects in question are data
    members of the
    object - these are not local objects because they exist for the lifetime of
    the owning
    object they belong to - although this approach is questionable when the
    object itself may be relocated
    and destroyed/invalidated ( such as in STL container classes). In such
    circumstances you might
    need to copy or process the return value immediately before its destroyed or
    invalidated.

    dave

    >
    > - Olumide


  • Next message: E. Robert Tisdale: "Re: Function Returning a local object???"

    Relevant Pages

    • Re: does python have useless destructors?
      ... >> between disposing at the end of the function or when some containing ... and in CPython that happens at the moment the last reference is ... C Python destroys an object when no references are left. ... IronPython destroy objects when the underlying garbage collector feels like ...
      (comp.lang.python)
    • Re: Application crashes on handled error. Why?
      ... but terminate events on those objects are not fired. ... call no more than one method and destroy immediately. ... >> I am not too happy about zapping a reference to a Parent in a Child ...
      (microsoft.public.vb.general.discussion)
    • Re: copy contructor
      ... "" and forget to put it in DESTROY, whereas my approach would look like: ... the memory leak isn't the worst problem. ... That's indeed a minor problem. ... Inside-Out object work because every the memory address of every reference ...
      (comp.lang.perl.misc)
    • Re: devctl (alike?) for devfs
      ... Typical holder of the reference is the devfs vnode. ... ls did trigger DESTROY notifications. ... I slightly don't like a fact that parent-child destroy notifications ...
      (freebsd-hackers)
    • Re: Strange Problems with VBA
      ... This causes a conflict when you try to rename a form to the ... "Forms" collection or run the test form and save a reference to the object ... it you can find the reference and destroy the form and set it's reference to ... "David C. Holley" wrote in message ...
      (microsoft.public.office.developer.vba)