[Qt-interest] QList, copy problems

Till Oliver Knoll till.oliver.knoll at gmail.com
Sat Oct 15 15:23:22 CEST 2011


Am 15.10.11 12:35, schrieb Thiago Macieira:
> On Saturday, 15 de October de 2011 12:06:44 Till Oliver Knoll wrote:
>> Am 15.10.2011 um 07:07 schrieb Sahana Bhaskar<Sahana_Bhaskar at infosys.com>:
>>> Any inputs will be appreciated. If I use operator= any modifications to
>>> the original list( after initializing ) is causing a crash!!!
>>
>> Ouch! = does only a shallow copy! In your case you might need a deep copy
>> however!
>
> Deep copies should never be necessary.

That statement totally doesn't make sense. It all depends on your 
use-case off course: if you want to have two completely separate 
"instances" of your list which you can modify without affecting the 
other instance, then you need a deep copy.

If on the other hand you want the items in the list to be unique, then 
you make a shallow copy, but you have to be aware that when you delete 
those instances your other copy of the list becomes invalid.

But you can't say the one usage makes more sense than the other usage.

And according to the description of the OP it just appeared to me that 
this is what happens ("lists of pointers to some objects in memory, 
shallow copy being done, you modify one list, *bang!*" sounds pretty 
much like "the usual suspects").

Hence my formulation "you *might* need a deep copy", as we simply have 
not enough information about the particular use-case. And we don't know 
how and when the list items are deallocated.

Cheers, Oliver



More information about the Qt-interest-old mailing list