问题
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.
回答1:
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).
来源:https://stackoverflow.com/questions/55071969/pointer-to-an-element-in-qlist