[Interest] Help, please !!!

Andre Somers andre at familiesomers.nl
Thu Apr 26 22:51:50 CEST 2012


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.

Your own code proves that you *can* modify the data inside the nested 
lists. But of course, everything you *put* in any list, is a copy. That 
also goes for lists that you put in a list-of-lists.

André



More information about the Interest mailing list