ilya1725
ilya1725

Reputation: 4938

Pointer to an element in QList

I have a list of lists container with this type:

QList< QList<UAVObject *> > objects;

For some reason I would like to access one of the internal lists quickly. Can I store a pointer to an internal list? For example:

QList<UAVObject *>& ref = objects[0];
QList<UAVObject *>* pt = &ref;

Will the value of pt still be valid across different function calls and original objects manipulations? Let's assume that the objects list will only be added to and never removed from.

Upvotes: 1

Views: 2129

Answers (1)

Dimitry Ernot
Dimitry Ernot

Reputation: 6584

QList is not based on a STL container and has nothing to do with std::list. The invalidation rules specified for the STL containers are not applicable.

According to the Qt documentation (and my understanding), it uses an array of pointers to the items, unless the item is not larger than a void* and declared as movable (with Q_MOVABLE_TYPE).

In that case, the item is directly stored in the array and the reference will become invalid when your list will grow (the array of pointers will be reallocated).

Upvotes: 2

Related Questions