Boost logo

Boost Users :

From: Hickman, Greg (greg.hickman_at_[hidden])
Date: 2002-11-10 13:34:52


I'd like to prevent clients from directly creating and deleting instances of
a type like 'Bill' below, so Bill's constructor and destructor have been
made private to prevent ordinary client access. Since Bill has reference
semantics, construction is handled by a static 'Bill::create()' factory
method which returns a dynamically allocated Bill instance wrapped in a
shared_ptr. Destruction is handled by a private Bill::Deallocator who also
happens to be a Friend of Bill (FOB).

My question is: Is this an acceptable use of the shared_ptr deallocator, or
am I just asking for trouble?

Thanks,
Greg Hickman

//--------------------------------------

#include <boost/shared_ptr.hpp>

using namespace boost;

class Bill;
typedef shared_ptr<Bill> Bill_ptr;

class Bill {
private:
  Bill() {}
  ~Bill() {}

  struct Deallocator {
    void operator() (Bill* b) const {
        delete b;
    }
  };

  friend struct Deallocator;

public:
  static Bill_ptr create() {
      return Bill_ptr(new Bill, Deallocator());
  }
};


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net