Boost logo

Boost :

From: Marcin Kaliciñski (kalita_at_[hidden])
Date: 2005-04-02 18:30:06

Hi Everybody,

Smart pointers in boost work on a principle of preventing object destruction
while references to it exist anywhere in the system. It causes that whoever
owns a boost::shared_ptr to some object, has power to decide for how long
will that object live. This is "ownership".

In some situations it would be more convenient if object itself decided when
to die (owned itself?), and notified all reference holders about this fact,
so that they not try to use it any more.

A real-life example from a computer game code. There are C++ objects
representing tanks on the battlefield. When tank gets hit, it explodes and
ceases to exist. The nice, straighforward way to do it is to write "delete
this;" in C++. All references to deleted tank which are held by other tanks
(for example those currently aiming at it, or following it), will be
immediately updated to NULL, provided that they are live_ptrs:

template<class T>
class live_ptr
  // Member functions closely resembling those of shared_ptr
  // When object pointed to is deleted, this pointer resets its value to

class Tank;

void live_ptr_test()
  Tank *tank = new Tank;
  live_ptr<Tank> p(tank); // not ownership, but a "live" reference
  delete tank;
  assert(p == 0);

I have been using (gradually improved) variations of such a smart pointer in
some of my projects for last couple of years, and found it useful. Anybody

Marcin Kalicinski

Boost list run by bdawes at, gregod at, cpdaniel at, john at