Pointer to an element in QList

佐手、 提交于 2019-12-24 07:56:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!