Re: Function Returning a local object???
From: Rich Herrick (rich.delete.me_at_richherrick.com.delete.me.too)
Date: 09/06/04
- Next message: Dave Townsend: "Re: Function Returning a local object???"
- Previous message: Alf P. Steinbach: "Re: How can I achieve this behaviour?"
- Next in thread: Dave Townsend: "Re: Function Returning a local object???"
- Maybe reply: Dave Townsend: "Re: Function Returning a local object???"
- Maybe reply: E. Robert Tisdale: "Re: Function Returning a local object???"
- Maybe reply: Gregg: "Re: Function Returning a local object???"
- Maybe reply: velthuijsen: "Re: Function Returning a local object???"
- Maybe reply: Gregg: "Re: Function Returning a local object???"
- Maybe reply: Old Wolf: "Re: Function Returning a local object???"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 06 Sep 2004 00:37:20 GMT
"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
>
> - Olumide
The code is fine. It is not returning the local, but a copy. The return
type is the class, not a reference.
- Next message: Dave Townsend: "Re: Function Returning a local object???"
- Previous message: Alf P. Steinbach: "Re: How can I achieve this behaviour?"
- Next in thread: Dave Townsend: "Re: Function Returning a local object???"
- Maybe reply: Dave Townsend: "Re: Function Returning a local object???"
- Maybe reply: E. Robert Tisdale: "Re: Function Returning a local object???"
- Maybe reply: Gregg: "Re: Function Returning a local object???"
- Maybe reply: velthuijsen: "Re: Function Returning a local object???"
- Maybe reply: Gregg: "Re: Function Returning a local object???"
- Maybe reply: Old Wolf: "Re: Function Returning a local object???"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|