指向 QList 中元素的指针 [英] 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;
pt
的值在不同的函数调用和原始对象
操作中仍然有效吗?让我们假设 objects
列表只会被添加到并且永远不会从中删除.
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.
推荐答案
QList
不是基于 STL 容器,与 std::list
无关.为 STL 容器指定的失效规则不适用.
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.
根据 Qt 文档(以及我的理解),它使用指向项目的指针数组,除非项目不大于 void*
并声明为 movable(使用 Q_MOVABLE_TYPE).
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).
这篇关于指向 QList 中元素的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!