Boost logo

Boost Users :

From: Kevin Heifner (heifner_k_at_[hidden])
Date: 2006-03-04 23:10:10


Arun Dharankar wrote:
> The type of shared pointer I have in mind is boost::shared_ptr. Is there
> some way I can extend/modify (short of re-writing) cleanly its functionality
> such that when its reference counter becomes zero, a method in the
> actual pointer's class is called instead of that class's destructor
> being called?
>
> Consider the following snippet to allow me to explain:
>
> class A {
> public:
> A() { }
> ~A() { }
> void deleteA() { }
> };
>
> boost::shared_ptr<A> a(new A);
> a.reset();
>
> In this example, when a.reset() is called A's I want the deleteA() method to
> be called instead of the destructor for A. It would then be up to
> deleteA() to
> decide if/when to cause the delete of the real object.

boost::shared_ptr supports this through a second argument to its
constructor.

boost::shared_ptr<A> a(new A, boost::bind(&A::deleteA, _1));

KevinH

-- 
Kevin Heifner  heifner @ ociweb.com  http://heifner.blogspot.com
           Object Computing, Inc. (OCI) www.ociweb.com

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