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)
|