QList shared class behaviour question
-
I do not have Qt/compiler at hand, so excuse if I am wrong or could just have tested. This is a quick "brainstorm" thought....
As one of Qt's implicitly shared classes I believe something like either of the following is allowed and causes sharing/no copy at least initially:
const QList a = { ... } ; const QList b = a; QList a = { ... } ; QList b = a;
I want a list from the outside world which is read-only to me, I want to create a list from it which I may modify but may not. There will be many of these lists and operations so speed is important. What about:
const QList a = { ... } ; QList b = a;
Assuming (untested) the second line compiles, does it still start out with a shared-no-copy until I modify it or does copying a
const
to a non-const
do a copy right from the assignment time?P.S.
While I am here. There isn't a very quick/easy/dirty way to see if aQList
is or isn't shared with another instance is there? Like showing the ref count or which list they really each are? (This would be for debugging/performance.) I guess I have to go==
to find out, I believeQList==
does a "are these the same shared list thing" rather than comparing each item? -
What are you afraid of here? That the 'copy from' list is const? Doesn't matter - the internal ref count is increased no matter if the container is const or not.
No, you can't see the refcount in simple way.
-
What are you afraid of here? That the 'copy from' list is const? Doesn't matter - the internal ref count is increased no matter if the container is const or not.
No, you can't see the refcount in simple way.
@Christian-Ehrlicher said in QList shared class behaviour question:
That the 'copy from' list is const? Doesn't matter - the internal ref count is increased no matter if the container is const or not.
Yep, just that. That it might have done an actual copy "in advance" when assigning a
const
to a non-const
. So it will indeed still start out shared, I am happy for my purpose where there will be many copies but few modifications. -
J JonB has marked this topic as solved on