Boost logo

Boost Users :

From: Stephen Crowley (stephenc_at_[hidden])
Date: 2002-11-25 17:15:49


I had this question a few weeks ago and there wasn't really a solution so I
wrote my own set of classes derived from shared_ptr.

http://systematicattack.net/smart_ptr.h

I'm using this in a fairly large project right now and I think everything is
working properly. I have also included a class called observer which is
derived from weak_ptr. Here is an example of it's usage

The only problem with it currently is that the smart_class MUST be managed
with smart_ptrs, if you try to 'delete' it, it will try to delete itself and
crash. Also, you cannot instantiate a smart_class on the stack, same
problem.. There might be a way to get around this, but it isn't a priority
for me yet.

I hope this is of some help.

--Stephen

class MyObject : public smart_class<MyObject>
{
 void something();
}

MyObject::something()
{
 blah = new Whatever();
 blah->member = this; // this works
}

main()
{
 MyObject *mo = new MyObject;
 smart_ptr<MyObject> sp(mo);
 // or
 sp = mo;
 observer<MyObject> o = sp;
 // or
 o = sp;
 o = mo;
 
}

On Mon, Nov 25, 2002 at 03:01:28PM -0600, Hickman, Greg wrote:
> I need an object managed through a shared_ptr to be able to pass itself
> (i.e., this) to another object which also expects a shared_ptr. Is there a
> preferred implementation technique for this when using the boost smart
> pointers?
>
> I think I've seen conversation in the past about deriving from
> boost::counted_base and calling shared_from_this(), but I think I also
> remember seeing comments to the effect that this is going away. OTOH, I
> know there's something called intrusive_ptr that's currently undocumented.
>
> Should I be using one of these approaches? If the answer is intrusive_ptr,
> are there docs? Should I roll my own? What's a Booster to do?
>
> -Greg


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