C++ smart pointers: sharing pointers vs. sharing data
- by Eli Bendersky
In this insightful article, one of the Qt programmers tries to explain the different kinds of smart pointers Qt implements. In the beginning, he makes a distinction between sharing data and sharing the pointers themselves:
First, let’s get one thing straight:
there’s a difference between sharing
pointers and sharing data. When you
share pointers, the value of the
pointer and its lifetime is protected
by the smart pointer class. In other
words, the pointer is the invariant.
However, the object that the pointer
is pointing to is completely outside
its control. We don’t know if the
object is copiable or not, if it’s
assignable or not.
Now, sharing of data involves the
smart pointer class knowing something
about the data being shared. In fact,
the whole point is that the data is
being shared and we don’t care how.
The fact that pointers are being used
to share the data is irrelevant at
this point. For example, you don’t
really care how Qt tool classes are
implicitly shared, do you? What
matters to you is that they are shared
(thus reducing memory consumption) and
that they work as if they weren’t.
Frankly, I just don't undersand this explanation. There was a clarification plea in the article comments, but I didn't find the author's explanation sufficient.
If you do understand this, please explain. What is this distinction, and how are other shared pointer classes (i.e. from boost or the new C++ standards) fit into this taxonomy?
Thanks in advance