I’ve seen two different non-intrusive approaches to this:
- The smart pointer allocates a small
block of memory to contain the
reference counter. Each copy of the
smart pointer then receives a
pointer to the actual object and a
pointer to the reference count. - In addition to an object pointer,
each smart pointer contains a
previous and next pointer, thereby
forming a doubly-linked list of
smart pointers to a particular
object. The reference count is
implicit in the list. When a smart
pointer is copied, it adds itself to
the list. Upon destruction, each
smart pointer removes itself from
the list. If it’s the last one in
the list it then frees the
referenced object as well.
If you go here and scroll to the bottom, there is an excellent diagram which explains these methods much more clearly.