As I said in another e-mail I didn't notice this is regarding an object
created inside the function and local to it. As usual C++ has some dark
sides to it and references may be just as evil as pointers. This is just
such a case.

I haven't looked at the questioned code so I cant offer a real suggestion
as to a solution specific to it.

On Sat, 21 Oct 2000, Andre Poenitz wrote:

> > If it's a large object that has long c-tor time the best method is to
> > return a const refernce as in: Menu const & expand(Buffer *) const;
> 
> You are not suggesting to return a reference to a local variable, are you?
> If so, this is the most wicked suggestion ever made - including those made
> on a Friday ;-}
> 
> Once the function is left, there is no guarantee that the stack space 
> used by it contains any sensible data. Hell, there is not even a guarantee
> your locals have been on "the stack", they could have been anywhere and
> are overwritten in the meantime by a concurrent process/thread/whatever.
> 
> The fact that it "usually just works" is pure luck. I'd even say "bad luck". 
> 
> If you need the object you got to bite into the sour apple and copy it.
> 
> Andre'
> 
> PS: And as I noticed in another post: Copying is not bad _unless your
> profiler has shown otherwise_.
> 
> 

-- 
  Baruch Even

http://techst02.technion.ac.il/~sbaruch/   (My Site)
http://www.redrival.com/jindor/            (My brothers AD&D site)

" Learn to laugh ... it's the path to true love! " 
   - The Angel in the movie Michael


Reply via email to