Holger Brunn wrote:
Sascha Volkenandt wrote:
Why would you want this, or better why should two cStrings point to the same buffer?
Thanks for your reply, after putting together an example, I found that my problem is rather a symptom. Look at this code:
#include "tools.h"
cString str=cString("hello world");
void func(cString string) { str=string; }
int main(int argc, char* argv[]) { printf("%s\n", *str); func(str); printf("%s\n", *str); }
The problem is that str and string in func point to the same buffer. And even without assinging string to str, the second printf receives a freed buffer, for cString's destructor will be called for string when func returns.
Then apart from dealing with the same-buffer thing, shouldn't cString have a copy constructor to take care of duplicating the buffer for this case? Or is cString intended to be passed by reference only?
Thanks for comments Holger
A copy ctor would certainly be a good idea. Please send a patch and let us know if that fixes your problem.
Klaus