PDA

View Full Version : [solved]QString.replace()



Qiieha
16th September 2011, 09:10
Hi
I have a little question.
I have a String in my Program and this String should be immutable. But some methods have to replace something in the String and return it.
The problem is, that QString.replace returns a reference and so the immutable String is also changed.

Why replace returns a reference to the String instead of a proper String. So I have to copy the immutable String to another Variable and this is indricate.

stampede
16th September 2011, 09:15
this String should be immutable. But some methods have to replace something in the String
So which one is it ? For me those two options are not compatible.

So I have to copy the immutable String to another Variable and this is indricate
Its quite straightforward :


QString str = my_const_string; // no copy made here, because of implicit sharing
str.replace(a,b); // my_const_string is left untouched, str gets its own copy of data and alters it