April 2018
Beginner
714 pages
18h 21m
English
First, implicit sharing means that holding any references or pointers to the container's content is disallowed when there is a possibility of changing this object or any object that shares the same buffer. The following small example illustrates the problem:
// don't do this!
QVector<int> x { 1, 2, 3 };
int *x0 = x.begin();
QVector<int> y = x;
x[0] = 42;
qDebug() << *x0; // output: 1
We initialized the x0 variable with the pointer to the first element of the x vector. However, when we set a new value for that element and then tried to read it using the pointer, we got the old value again.
Read now
Unlock full access