TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JAN BIJSTERBOSCH
from: ALBERTO MONTEIRO
date: 1997-12-23 22:32:00
subject: What`s wrong with this?

JB>In an earlier posting I saw a definition of an overloaded + operator as
JB>follows:
JB>String operator+ (String &left, String &right)
JB>{
JB>    String str;
JB>    ...
JB>    return str;
JB>}
JB>In my humble opinion this will not work on all compilers (however Borland 
wi
JB>except it I think) because the moment of deallocation of str is not 
efined
JB>the C++ standard.
JB>I think first of all this operator should be declared as a friend in the
JB>String class and read as follows:
JB>friend String & operator+ (const String &left, const String &right);
I've tested the option above, and it doesn't work with M$ Visual C++.
JB>The returning of a reference will create a temporary object thus making 
he
JB>moment of deallocation of str unimportant.
But it will reference to something temporary, when this temporary object
vanishes, the reference will get crazy.
In my original question, these wild pointers caused some memory
corruption that made even good code crash.
Alberto Monteiro
---
 þ SLMR 2.1a þ Deceit, Deceive, Decide just what you believe (Metallica)
--- FMail/386 1.02
---------------
* Origin: CentroIn! +55-21-205-0281, 41 lines, 24h, RJ/Brazil (4:802/21)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.