[Interest] Help, please !!!

André Somers andre at familiesomers.nl
Fri Apr 27 10:14:35 CEST 2012


Op 26-4-2012 23:15, Nikos Chantziaras schreef:
> On 26/04/12 23:51, Andre Somers wrote:
>> Op 26-4-2012 18:47, Nikos Chantziaras schreef:
>>> On 26/04/12 19:01, André Somers wrote:
>>>> Op 23-4-2012 20:44, Nikos Chantziaras schreef:
>>>>> Then you're not doing what you think you're doing:
>>>>>
>>>>>          QList<     QList<int>     >     listOfLists;
>>>>>          QList<int>     listOfInts;
>>>>>          listOfInts.append(10);
>>>>>
>>>>>          listOfLists.append(listOfInts);
>>>>>          listOfLists[0][0] = 9;
>>>>>
>>>>>          qDebug()<<     listOfLists[0][0]<<     listOfInts[0];
>>>>>
>>>>> You are modifying a copy, so it prints"9 10"  instead of"10 10".  This:
>>>>>
>>>>>        listOfLists[0][0] = 9;
>>>>>
>>>>> modifies a copy of listOfInts.  Also the reverse is true.  If you modify
>>>>> listOfInts, then the copy of it inside listOfLists is not updated.
>>>>>
>>>>> "Implicit sharing"  means that data is copied when it's modified.  It's
>>>>> not a replacement for pointers.
>>>>>
>>>> Not true, in this case.
>>> I posted code that proves my point.  It prints "9 10".  You can't argue
>>> with that one ;-)
>> The copy is made at the moment you _append_ the list, not at the moment
>> you're modifying it. That's a big difference. So yes, your code prints
>> "9 10", and it should! However, the issue was about if you could modify
>> what's in that nested list. You said:
>>    >   Note that with QList<   QList<int>   >   you can't modify the other lists.
>> You'd only be modifying the copies.
> Nope.  That was not the issue.  The issue was that when you append list
> A into list B, you can't get to list A because what's inside list B is a
> copy of A, not A itself.  You might want to read again from the first post.
>
Ok, sure. Anything you put in a container is a copy of whatever you put 
there. True. So, your container does not contain A, it contains A'. It 
is irrelevant for the disucssion if whatever we put in the container has 
CoW semantics or not. So yes, in that sense you are right: you cannot 
acces A via the container. Note that the same is true for using 
pointers: the container holds a copy of the pointer, and if you change 
it (the pointer itself, not the pointee obviously), the original will 
not change with it:

QList<int*> intList;
int i(42);
int* iPtr = &i;
int j(101);
int* jPtr = &j;
intList << iPtr; //yes, I know this is unsave, that's not the point.
intList[0] = jPtr; //no, iPtr did obviously not change here, as the 
container holds a *copy* of the pointer

int* intPtr = intList[0]; //intPtr == jPtr
*intPtr = 0; // j changed from 101 to 0, i is untouched.

All the above is obvious, and I don't see why it isn't obvious anymore 
if you subsitute using integers or pointers to integers for QList<int>.

I guess all we're dealing with here is a misunderstanding due to unclear 
communication.

André




More information about the Interest mailing list