TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JAN BIJSTERBOSCH
from: THOMAS MAEDER
date: 1997-12-24 14:46:00
subject: What`s wrong with this?

JB>  TM> explicit Vector(double xx = 0, double yy = 0);    //
JB>  TM> constructor
JB> To kill any compiler warnings, should this not better be written as:
JB> explicit Vector(double xx = 0.0, double yy = 0.0);
Implicit conversion from int to double should not produce warnings.
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 Bo
JB> except it I think) because the moment of deallocation of str is not
JB> the C++ standard.
It is defined, but not relevant for the question:
return str;
copies str into an unnamed String variable. str is destructed when the
function is left.
When you do something like
const String s1("string1"), s2("string2);
const String s3(s1+s2);
the  copy  constructor  used  to initialize s3 gets a reference to the
unnamed String object. This unnamed object is guaranteed to live until
the  end  of  the  statement where operator+ is called.
JB> friend String & operator+ (const String &left, const String &right);
Declaring operator+ a friend may be a good idea depending on how class
String is implemented.
But  have  it  return  a reference is certainly an error. The returned
reference refers to the local variable (that you call str); in the two
lines I gave above, the copy constructor is thus passed a reference to
an already destructed object.
Thomas
---
 þ MM 1.0 #0113 þ Wagner's music is better than it sounds. - Twain
---------------
* Origin: McMeier & Son BBS (2:301/138)

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™.