Hi! I have a few foreach related questions below I hope that somebody could answer. I know that most people do not care about the possibly insignificant differences, but I want to understand them for completeness.
1) Could somebody explain how the foreach keyword in Qt actually works behind the scenes. I would like to know if foreach creates a new item object pointer and reevaluates the collidingItems (potentially expensive) on each iteration in the below example
foreach (QGraphicsItem *item, someItem->collidingItems(Qt::IntersectsItemShape) {
// do something with item;
}
and should it hence be typed like this
QGraphicsItem *item;
QList<QGraphicsItem *> itemList = someItem->collidingItems(Qt::IntersectsItemShape);
foreach (item, itemList) {
// do something with item;
}
I assume a no, since according to documentation it takes a copy of the list before entering the loop. However, if I recall correctly, at least a for statement evaluates the check on every iteration, so I just want to be sure.
2) Second question. Since foreach makes a copy of the list, is it ok to change the original inside the foreach as in
QList<QGraphicsItem *> itemList = someItem->collidingItems(Qt::IntersectsItemShape);
foreach (QGraphicsItem *item, itemList) {
if (item->type() != QGraphicsItem::UserType)
itemList.removeOne(item);
}
// continue using itemList
3) A final question. Is there any difference performance vise to precreate and reuse the pointer (shown below) or defining a new pointer inside foreach loop every time (assume a large list)?
MyGraphicsItem *myItem; // a complex subclass of QGraphicsItem
foreach (QGraphicsItem *item, someItem->collidingItems(Qt::IntersectsItemShape)) {
myItem = qgraphicsitem_cast<MyGraphicsItem *>(myItem);
// do something with myItem;
}
Thank you!