   object containing weak pointer to itself?
  Is there any standard method when dealing with classes that require a weak pointer to themselves? (To create safe callbacks)I often find myself doing something like thisclass Foo;typedef boost::shared_ptr<Foo> foo_sptr;typedef boost::weak_ptr<Foo> foo_wptr;class Foo{    foo_wptr this;    public:    Foo(args);    void AddWptr2This(foo_wptr newthisptr);    void Initialize();}foo_sptr CreateNewFoo(args){    foo_sptr temp(new Foo(args));    temp->AddWptr2This(temp);    temp->Initialize();    return temp;}but it seems unnecessarily complicated. Surely, there's a better way?   
  I'm struggling to think of any situation where an object would need a weak pointer to itself.  If the object is valid, then "this" is valid.  If the object is invalid (has been destroyed) then any member variable (including a weak pointer) is garbage data.-me   ;  enable_shared_from_this   ;Quote:Original post by PalidineI'm struggling to think of any situation where an object would need a weak pointer to itself.  If the object is valid, then "this" is valid.  If the object is invalid (has been destroyed) then any member variable (including a weak pointer) is garbage data.-meYeah but suppose Object A wants give a callback to Object B. If you use a raw pointer in the callback, and Object A is later deleted, you'll have a dangling pointer.   ;Quote:Original post by StoryyellerYeah but suppose Object A wants give a callback to Object B. If you use a raw pointer in the callback, and Object A is later deleted, you'll have a dangling pointer.ok.  But then wouldn't object A hold the weak pointer to Object B rather than Object B holding a weak pointer to itself?-me   ;Quote:Original post by PalidineQuote:Original post by StoryyellerYeah but suppose Object A wants give a callback to Object B. If you use a raw pointer in the callback, and Object A is later deleted, you'll have a dangling pointer.ok.  But then wouldn't object A hold the weak pointer to Object B rather than Object B holding a weak pointer to itself?I think he meant that B should hold a weak_ptr to A so that it can reliably call the callback. And if A is the one giving B the callback, it needs to create a weak_ptr to itself.   ;Quote:Original post by PalidineQuote:Original post by StoryyellerYeah but suppose Object A wants give a callback to Object B. If you use a raw pointer in the callback, and Object A is later deleted, you'll have a dangling pointer.ok.  But then wouldn't object A hold the weak pointer to Object B rather than Object B holding a weak pointer to itself?-meSorry, I guess that was worded incorrectly due to a typo. Object A wants to give a callback (to itself) to Object B.The callback contains a weak pointer to Object A. In order for Object A to create the callback, it has to contain a weak pointer to itself.As for enable_shared_from_this, it looks like exactly what I want. I still have to have a separate initialization function, but I guess that is unavoidable.One thing I am confused by is, why must boost::enable_shared_from_this<T> be inherited publicly? Why can't the inheritance be private or protected?   ;  The constructor for shared_ptr<> needs to do something different if the object pointed to is derived from enable_shared_from_this<>, and in order for it to be able to do that it needs to be a public inheritance.   ;  There's also this:void null_deleter(void *) { }class foo{    boost::shared_ptr<foo> self;public:    foo() { self.reset(this, null_deleter); } // n.b. Avoiding initialiser list to escape the: using 'this' in initialiser list warning.    boost::weak_ptr<foo> get_callback_ptr() { return boost::weak_ptr<foo>(self); }};It works fine as long as you don't expect any shared_ptr's acquired from locking the returned weak_ptr to share the same reference count as any shared_ptr's created elsewhere.You could also have the class be the one that produces shared_ptr's to itself:class foo{private:    boost::weak_ptr<foo> self;    foo();public:    static boost::shared_ptr<foo> create()    {   boost::shared_ptr<foo> p = boost::make_shared<foo>();   p->self = boost::weak_ptr<foo>(p);   return p;    }}; [Edited by - dmatter on April 7, 2010 11:55:14 AM];Quote:Original post by dmatterIt works fine as long as you don't expect any shared_ptr's acquired from locking the returned weak_ptr to share the same reference count as any shared_ptr's created elsewhere.What would be the point then? If the pointers distributed by the object can't be used to keep the object alive (i.e. the entire idea behind using reference counted smart pointers) then all this would do is introduce a source of bugs. 