Re: Function Returning a local object???
From: Dave Townsend (datownsend_at_comcast.net)
Date: 09/06/04
- Previous message: Rich Herrick: "Re: Function Returning a local object???"
- Maybe in reply to: Rich Herrick: "Re: Function Returning a local object???"
- Next in thread: E. Robert Tisdale: "Re: Function Returning a local object???"
- Reply: E. Robert Tisdale: "Re: Function Returning a local object???"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: Rich Herrick: "Re: Function Returning a local object???"
- Maybe in reply to: Rich Herrick: "Re: Function Returning a local object???"
- Next in thread: E. Robert Tisdale: "Re: Function Returning a local object???"
- Reply: E. Robert Tisdale: "Re: Function Returning a local object???"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|