[Qt-interest] QList, copy problems
Thiago Macieira
thiago at kde.org
Sun Oct 16 10:24:24 CEST 2011
On Saturday, 15 de October de 2011 19:32:44 Till Oliver Knoll wrote:
> Am 15.10.11 16:17, schrieb Thiago Macieira:
> >> ... 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.
> >
> > Not really. QList will make a deep copy the moment that you try to modify
> > it. So you don't need to make a deep copy before QList makes a deep copy
> > for you.
> Just for clarity: a "deep copy" is an operation where every element and
> sub-element thereof is copied, hence you end up with two distinct
> instances having the same structure.
>
> In particular when you "deep copy" a list then you end up with two
> instanes of a list, each pointing to /different/ items (which have been
> "deep copied" before).
That's not how I read it.
"Deep copy" for me is the sense of Qt 3's QDeepCopy class, which caused the
containers to stop sharing. But the classes contain otherwise the same element
values, so that:
orig_list == copied_list
Remember that QList's operator== executes operator== on each element. If the
current list is a list of pointers, pointer addresses will be compared. If you
cannot create new pointer objects as that would cause the lists to differ after
the operation and I wouldn't call that a "deep copy".
> So no, QList does /not/ make deep copies.
By my definition, it does when it detaches. In fact, in my example in the
previous email, it did. That's the copy-on-write you mentioned.
[snip]
> And what is copied? The value - in this case the pointer.
>
> However the thing the pointer points at is /not/ copied.
Correct.
> So if you want to avoid this, then you have exactly to do what Thiago
> mentioned: create a second QList instance, copy each object (not just
> the pointer!) with a "deep copy" (so if those instances point to other
> data, also "deep copy" that data etc.) and insert each copied object
> from list A into list B.
>
> And there you go: "deep copy" ;)
Agreed on the solution, disagreed on the name :-)
> > And my formulation of "deep copies are never necessary" is also correct.
> > It's not about the list's contents, but objects pointed to.
>
> Uhm well, actually I still kind of disagree with "deep copies are never
> necessary", but it might be because we have a different understanding of
> "deep copy" ;) But I think we mean the same thing above anyway ;)
I guess that's the case.
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20111016/4aee4198/attachment.bin
More information about the Qt-interest-old
mailing list