David Rubin said:
I've seen this suggested many times, but I've never read enough to
understand the benefits. My impression is that a "shared pointer" is an
object which encapsulates a pointer to the "real data." A copy of this
object is stored in the STL container. Since the container holds an
object (versus a pointer), the object destructor is called when the
container element is erase()d, and hence the real object is delete'd.
Different smart pointers have different benefits. But for this situation
reference counting would be used which means that the real object would only
be deleted when the destructor for the last copy of the smart pointer is
invoked. This has many benefits whether or not those smart pointers happen
to be in STL container. Boost's shared pointer works like this (I guess
that's what makes it a _shared_ pointer).
This is superior to storing raw pointers for which the caller must a)
hold a reference and coordinate object cleanup, or b) leak memory. Is
this at all accurate?
Not quite sure what you mean by hold a reference, but the rest seems
accurate enough. For me the big problem in holding raw pointer in STL
containers is that you can manage it easily enough if you have only a single
copy of the container but a soon as you start to want to make copies of the
container things rapidly get too complex and messy.
john