Home > Enterprise >  How to store qt containers inside qt containers
How to store qt containers inside qt containers

Time:02-25

Do I understand right that it does not make real sense to have

QVector<QSharedPointer<QVariantHash>>

and that I can stick just to:

QVector<QVariantHash>

due to implicit sharing?

Honestly, using STL, I would never do like this and would have std::vector<std::shared_ptr<std::unordered_map<...>>>.

UPDATE:

My question is about performance. I understand that it's different ways of storing objects.

CodePudding user response:

No, those have different behaviour.

The QVector<QVariantHash> is still copy-on-write, so copies of the vector only share elements up to the first modification, whereas so long as you leave the pointers alone, the QVariantHashs pointed-to by the elements of QVector<QSharedPointer<QVariantHash>> will still be the same objects.

As an aside, I would avoid relying on implicit sharing, because it is really easy to fall into undefined behaviour, with pointers or references being invalidated from underneath you.

I would also caution against overuse of shared pointers. Almost always you can have one thing with unique ownership which hands out references.

  • Related