[Discuss] OT - C++ operator overloading question.

David Bronaugh dbronaugh at linuxboxen.org
Thu Aug 31 00:15:35 PDT 2006


Adam Parkin wrote:
> I'm sure you probably already know this, but of course there is a 
> catch if you do something like:
>
> NumData & foo()
> {
>     NumData n(1,10);
>     // do some stuff with n
>     return n;
> }
>
> As n is destroyed after the return so once the caller of foo tries to 
> reference the return value of foo the program chokes (well, 
> technically the result is undefined, but bad either way).  And of 
> course if you change the return type of foo from "NumData &" to just 
> "NumData" then you're implicitly invoking the copy constructor of the 
> class which can be a significant performance hit depending on the 
> complexity of the class.
Yeah, but who cares? It'll work if you don't need to return it, and it 
significantly decreases code complexity.
> Yet another reason to use a decent language (ie one with garbage 
> collection). =;-p
GC doesn't make a language decent. Java is proof of that.

David


More information about the Discuss mailing list