Skip to main content
gamedev.net
🔒 Locked

strange behaviour...

Started by uNiQue0815 Jun 1, 2004 at 8:02 AM 0 replies 350+ views
Original Post
uNiQue0815
uNiQue0815
some time ago, i wrote a string class. today in one of its methods, the application suddenly crashed... i didn''t have problems with the string class for months now, so this seemed strange to me. after hours of debugging, i still could not make out a bug. so i rewrote a part of a function in my app, in which the string was used & crashed. suddenly the problem was gone... i looked a bit closer at my code and noticed, that there was a small difference : original (crashing on return):
void myFunction(const String &dir)
{
	String directory = dir;
	directory.insert(directory.GetLength(), ''/'');
}
 
''copy'' (working):
void myFunction(const String &dir)
{
	String directory;
	directory = dir;	// <<< ??
	directory.insert(directory.GetLength(), ''/'');
}
 
so what does that mean ?? shouldn''t the first method be exactly the same, as the second one ?
SiCrane
SiCrane
No, they aren''t the same. In the first version you copy construct the directory string from the dir string. In the second version you default construct the directory string and then assign the dir string to the directory string. Why that makes a difference would depend on how your assignment operator and copy constructors are defined for your string class.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.