Is intrusive_ptr the thing to use?
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
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
From: "Hickman, Greg"
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?
Yes, there is now. The possible solutions depend on the location where you want to extract a shared_ptr to 'this'. If you need to do this in the constructor, use a static factory function: class X { private: X(); public: static shared_ptr<X> create() { shared_ptr<X> px(new X); // do something with px return px; } }; If you need to obtain a shared_ptr to this in a nonvirtual member function X::f, consider replacing it with a free function: void f(shared_ptr<X> this_, ...); If 'f' is virtual, have the object store a weak_ptr to itself: class X { private: weak_ptr<X> weak_this; X(); X(X const &); X& operator=(X const &); public: static shared_ptr<X> create() { shared_ptr<X> px(new X); px->weak_this = px; return px; } shared_ptr<X> shared_from_this() { shared_ptr<X> px(weak_this); return px; } shared_ptr<X const> shared_from_this() const { shared_ptr<X const> px(weak_this); return px; } }; The current CVS version of shared_ptr includes explicit support for this idiom, a base class template enable_shared_from_this<> that supplies the shared_from_this member function. It's even documented. ;-)
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.
Yes, you are right. This is going away.
participants (3)
-
Hickman, Greg
-
Peter Dimov
-
Stephen Crowley