隱含共享

由於隱式共享,Qt 容器上的 STL 樣式迭代器可能會產生一些負面影響。建議在迭代器處於活動狀態時避免複製 Qt 容器。

QVector<int> a,b; //2 vectors
a.resize(1000); 
b = a; // b and a now point to the same memory internally

auto iter = a.begin(); //iter also points to the same memory a and b do
a[4] = 1; //a creates a new copy and points to different memory.
//Warning 1: b and iter point sill to the same even if iter was "a.begin()"

b.clear(); //delete b-memory
//Warning 2: iter only holds a pointer to the memory but does not increase ref-count. 
//           so now the memory iter points to is invalid. UB!