
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()); } };

On Sunday, November 10, 2002, at 10:34 AM, Hickman, Greg wrote:
My question is: Is this an acceptable use of the shared_ptr deallocator, or am I just asking for trouble?
It's just fine. Recommended even. This is one of the strengths of shared_ptr, that shared_ptr instances can be deleted even in contexts where the deallocating function is not accessible. -- Darin
participants (2)
-
Darin Adler
-
Hickman, Greg