[Qt-interest] Iterators and implicitly shared containers
Malyushytsky, Alex
alex at wai.com
Fri Dec 17 23:48:45 CET 2010
>>however, the *it=2; should recognize that the ref count > 1, and
>>do a detach/deep copy as necessary no??
>>
>>Soctt
This is what I would expect,
but I afraid that you can't achieve it at least with current implementation of iterators/containers sharing in Qt.
The way to fix your code is to obtain iterator after assignment.
Modification below will produce results you expected, cause detachment will be done when v1.begin() is called.
If I were you I would try to report the bug. If not fixed it will be good at least to have a special warning in documentation which identifies possible problem.
#include <QtCore/QCoreApplication>
#include <QtDebug>
#include <QVector>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QVector<int> v1 = QVector<int>() << 1;
const QVector<int> v2 = v1;
QVector<int>::iterator it = v1.begin();
*it = 2;
//v1[0] = 2;
qDebug() << "v1:" << v1 << ", v2:" << v2;
return 0;
}
Regards,
Alex
---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.
“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”
“Please consider our environment before printing this email.”
More information about the Qt-interest-old
mailing list