When does a deep copy happen to a QList?

前端 未结 3 826
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-11 16:27

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 understan

3条回答
  •  天涯浪人
    2021-01-11 16:46

    QLists are implemented using implicit sharing.

    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.

    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.

提交回复
热议问题