This is because QStringList is one of the implicit shared classes of Qt, which means that when you make a copy, you actually copy only the link to the contents of the list ("real" copying only happens when / if one of the objects changes).
If you want an iteration like you are here, you have to change the end () method with constEnd (), like this:
QStringList::const_iterator it = list.constEnd(); if (b) { // 'static' to prevent optimization static QStringList copy = list; } --it; // 2 --it; // 1 --it; // 0 ++it; // 1 ++it; // 2 --it; // 1 ++it; // 2 ++it; // end return it == list.constEnd();
While the end () method returns an iterator, constEnd () (or cend ()) returns a const_iterator, so this is what you need to use in this case. There's an interesting article there if you want to explore more deeply:
http://doc.qt.digia.com/qq/qq12-qt4-iterators.html#implicitsharinganditerators
I hope this can help you.
source share