Implicitly Shared Classes
A hybrid kind of managed container is the implicitly shared container. Container classes in the Qt library implement "lazy copy on write," or implicit sharing. This means they have reasonably fast[3] copy constructors and assignment operators. Only when the copy is actually modified are the collected objects cloned from the original container. That is when there will be a time/memory penalty.
[3] Operations should be "not much slower" than the time it takes to do one pointer-copy and one integer increment.
QString and QStringList are both implemented this way, meaning that it is fast to pass and return these objects by value. If you need to change values stored in the container from inside a function, you can pass the container by reference. It is still faster to pass by const reference, which allows C++ to optimize out the copy operation entirely. With const reference, the function cannot make changes to the container at all.
Implicitly shared classes work by reference-counting, to prevent the accidental deletion of shared managed objects. Since implicitly shared memory is encapsulated, the user of the class does not need to be concerned with reference counts or direct memory pointers.
We look in more detail at the implementation of a reference counted class in Section 24.2.