Listing 1

#include <utility>
#include <iostream>
#include <vector>
using std::cout; using std::vector;
using std::tr1::shared_ptr;

struct instrumented
  { // report construction and destruction
  instrumented()
    { cout << " created object at: " << (void*)this << '\n'; }
  ~instrumented()
    { cout << " destroyed object at: " << (void*)this << '\n'; }
  };
typedef shared_ptr<instrumented> sp;
static void use_vector(sp ptr)
  { // push shared_ptr objects into vector
  vector<sp> vec;
  vec.push_back(ptr);
  cout << "creating two objects\n";
  for (int i = 0; i < 2; ++i)
    vec.push_back(sp(new instrumented));
  }
int main()
  { // demonstrate reference counting
  cout << "creating one object\n";
  sp ptr(new instrumented);
  cout << "calling use_vector\n";
  use_vector(ptr);
  cout << "returned from use_vector\n";
  return 0;
  }