Using QVector and implement a subset of QVector in good practice

259 views Asked by At

Hello and good morning together, I have two questions concerning QVector and its usage. I have an own custom class. It is necessary to use QVector<QSharedPointer<Class*>> or does it suffice to directly add instances to QVector like QVector<Class*>>. I read that QVector already uses a shared pointer internally contrary std vector. It is bad practice to append instance pointer directly?

Next, I want to have a subset of QVector with selected elements. What is good practice to do that using QVector>?

 __________
|QVector   |
|   _______|
|  |QVector|
|__|_______|
1

There are 1 answers

0
AudioBubble On

Mayble it's not gut idea to use QVector to directly add instances. Better solution is using QList container.

It does not store objects directly, but instead stores pointers to them. You gain all the benefits of quick insertions at both ends, and reallocations involve shuffling pointers instead of copy constructors, but lose the spacial locality of an actual or , and gain a lot of heap allocations.

QList<QList<Class>> testlist, and don't forget to override operator =