OUString is mutable?

Michael Stahl mstahl at redhat.com
Fri Sep 28 07:44:42 PDT 2012


On 28/09/12 16:05, Noel Grandin wrote:
> 
> On 2012-09-28 16:00, Caolán McNamara wrote:
>> On Fri, 2012-09-28 at 14:17 +0200, Noel Grandin wrote:
>>> you can do this:
>>>
>>>       void f(OUString s) {
>>>            s = "2";
>>>       }
>>>
>>>       OUString s = "1";
>>>       f(s);
>>>       cout << s; // will print "2"
>> That will print "1" not "2".
>>
>> Maybe you meant
>>
>> void f(OUString& s) {
>>    s = "2";
>> }
>>
>> OUString s = "1";
>> f(s);
>> cout << s; // will print "2"
>>
>> but that's perfectly reasonable.
>>
>> C.
>>
> 
> 
> Yeah, that's what I meant.
> But that's also what I have a problem with.
> It means that any OUString field or variable is effectively mutable, 

i don't see how that is the case.  the calling code needs to explicitly
pass the string as a parameter to a function for it to be modified, and
it's clear from the parameter non-const reference type that this can
happen.  you can also pass an ordinary pointer by-reference and then the
called function can reset it to a different value, that is just how C++
works...:

void f(void *& p) {
	p = (void*)42;
}

for comparison, in Java you cannot do this directly (because Java does
not have call-by-reference semantics, only call-by-value), you have to
wrap the string in e.g. an array to get something similar:

void f(String[] s) {
	s[0] = "bar";
}




More information about the LibreOffice mailing list