Re: cString operator= with same buffer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sunday 20 November 2005 20:53, Jon Burgess wrote:
> What happens if someone was trying to remove the initial part of the
> string, e.g. something like:
>
> 	cString hw("Hello World");
> 	hw = strstr(*hw, " ")+1;

In that case the following would happen (with the original cString plus a 
simple copy ctor):
1. strstr gets the "second word" of "Hello World" (which is part of hw's 
buffer)
2. a temporary cString is constructed with that result (because cString has no 
operator=(const char*), only an operator=(const cString&) ) making the 
"second word" a copy of the second half of hw's buffer
3. operator=(const cString&) frees hw's old buffer (which is not referenced by 
the temporary) and replaces it with a copy of the temporary cString's buffer
4. the temporary cString is destructed freeing it's buffer

So, no unexpected behaviour here.

Like I mentioned before, I don't think all of this is necessary since the 
normal ctor and the copy ctor cover all cases. Besides, I've never 
encountered string classes doing such overly complicated things to handle 
their buffers, including robust frameworks such as QT or MFC.

Greetings,
Sascha


[Index of Archives]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Util Linux NG]     [Xfree86]     [Big List of Linux Books]     [Fedora Users]     [Fedora Women]     [ALSA Devel]     [Linux USB]

  Powered by Linux