T.Day@CS.UCL.AC.UK (Tim Day) (05/10/89)
I am rather confused by the following...
Most of the time, a String() seems to be the equivalent of a String(""),
(as I'd expect), but in the below code attempting to output an unitialized
String returned from a function as a String causes a segmentation fault,
which weird since normally uninitialised Strings, and functions returning
Strings, work fine. It also seems odd that returning a StrTmp should fix
the problem. (As will always declaring
String bleah;
as
String bleah("");
)
------------------------------
#include <stream.h>
#include <String.h>
String hot() // Returning un-init. String as String bombs
{ String bleah;
return bleah;
}
StrTmp digital() // ..but returning it as StrTmp is OK
{ String bleah;
return bleah;
}
String dog() // ..as is returning it as String, once loaded
{ String bleah;
bleah=String("WOOF");
return bleah;
}
main()
{ if (String() == String(""))
cerr << "Uninitialised string == empty string\n";
if (String() != String(""))
cerr << "Uninitialised string != empty string\n";
cerr << "'" << String("") << "'\n"; // OK
cerr << "'" << String() << "'\n"; // OK
cerr << "'" << dog() << "'\n"; // OK
cerr << "'" << digital() << "'\n"; // OK
cerr << "'" << hot() << "'\n"; // Ouch!
exit(0);
}
-------------------------------
Tim