summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets/code/doc_src_containers.cpp
diff options
context:
space:
mode:
authorThorbjørn Martsum <tmartsum@gmail.com>2013-09-24 16:42:22 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-10-15 12:10:01 +0200
commit499957eb8b1c5150823f05f40f32b04ba13b1640 (patch)
treee339c9fdf4d0121f175e35bed4883007190ed971 /src/corelib/doc/snippets/code/doc_src_containers.cpp
parentcfb717d654b92dcfde94cddc7039abeafe67f289 (diff)
Improve implicit shared documentation a bit
Task-number: QTBUG-27061 Change-Id: I66e000a9f59fda3654066013e6e78c3ba6fd27fe Reviewed-by: Mitch Curtis <mitch.curtis@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/doc/snippets/code/doc_src_containers.cpp')
-rw-r--r--src/corelib/doc/snippets/code/doc_src_containers.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/doc_src_containers.cpp b/src/corelib/doc/snippets/code/doc_src_containers.cpp
index 350b2a91f2..6e59a8a548 100644
--- a/src/corelib/doc/snippets/code/doc_src_containers.cpp
+++ b/src/corelib/doc/snippets/code/doc_src_containers.cpp
@@ -273,3 +273,33 @@ QString onlyLetters(const QString &in)
return out;
}
//! [23]
+
+//! [24]
+QVector<int> a, b;
+a.resize(100000); // make a big vector filled with 0.
+
+QVector<int>::iterator i = a.begin();
+// WRONG way of using the iterator i:
+b = a;
+/*
+ Now we should be careful with iterator i since it will point to shared data
+ If we do *i = 4 then we would change the shared instance (both vectors)
+ The behavior differs from STL containers. Avoid doing such things in Qt.
+*/
+
+a[0] = 5;
+/*
+ Container a is now detached from the shared data,
+ and even though i was an iterator from the container a, it now works as an iterator in b.
+ Here the situation is that (*i) == 0.
+*/
+
+b.clear(); // Now the iterator i is completely invalid.
+
+int j = *i; // Undefined behavior!
+/*
+ The data from b (which i pointed to) is gone.
+ This would be well-defined with STL containers (and (*i) == 5),
+ but with QVector this is likely to crash.
+*/
+//! [24]