Re: Quick question on returning references to local variables
ankur wrote:
My question is , in the code below , how is it that the last ob1.show
() displays 15 50, even though the temp object in overloaded operator
function has been destroyed as illustrated by the destructor call.
The code you posted invokes undefined behaviour, so anything is possible,
including "correct" behaviour.
What probably happens is that the memory remained the same, so just looking
at the memory where there once was an object looks like the object. Clobber
the memory in the destructor and you will see that the object was
destroyed.
loc& operator+(loc op2);
Hint: implement operator+ as a free function:
loc
operator+(loc const& l1, loc const& l2)
{
loc res = l1;
res += l2;
return res;
}
Then, implement operator+= as a memberfunction.
Uli
--
Sator Laser GmbH
Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]