什么时候对QList进行深拷贝? [英] When does a deep copy happen to a QList?

查看:1351
本文介绍了什么时候对QList进行深拷贝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在上的一堂课中,我在玩弄几个QList.我听说Qt尽可能不制作列表的深层副本.据我了解,这意味着在执行此操作时不会发生任何深层复制:

In a class I'm working on, I am juggling several QLists. I have heard that Qt tries not to make deep copies of lists whenever possible. From what I understand, that means that no deep copy happens when you do this:

QList<int> myList;
myList << 1 << 2 << 3 << 4;
QList<int> otherList = myList;  // No deep copy

在某些情况下,我需要确保QList绝不会复制深层副本.为了确保我所使用的QList绝不会复制深层副本,我到底需要避免哪种操作或动作?

In some cases, I need to make sure a deep copy never happens to the QList. Exactly what kind of operation or action do I need to make sure to avoid in order to make sure a deep copy never happens to a QList I'm working with?

推荐答案

QList使用

隐式共享对象的对象分配(使用operator =())为 使用浅表副本实现.

Object assignment (with operator=()) for implicitly shared objects is implemented using shallow copies.

这意味着仅分配就不会导致所包含的数据被复制.但是,写入共享实例 将导致源对象被复制.这种模式通常称为写时复制".

This means that assignment alone will never cause the contained data to be copied. However, writing to a shared instance will cause the source object to be copied. This pattern is commonly known as copy-on-write.

因此,为回答您的问题,如果您从不写入共享实例,则将永远不会复制它们.如果要完全防止复制,请从QList派生并重写并隐藏复制构造函数和赋值运算符.

So, to answer your question, if you never write to shared instances then they will never be copied. If you want to completely prevent copies being made then derive from QList and override and hide the copy constructor and assignment operator.

这篇关于什么时候对QList进行深拷贝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆