Re: [boost] shared_ptr : how to build object chain shared_ptr down and weak_ptr up in object ctor?

On Wed, September 20, 2006 11:48 am, Tom Titchener wrote:
I'd like to build a chain of objects with an upper layer holding a shared_ptr to a lower layer and the lower layer holding a weak_ptr back to the upper layer. The upper layer users the shared_ptr for control. The lower layer uses the weak_ptr for callbacks (first calling lock to convert it to a shared_ptr). And ... I'd like to do it in my class ctors. That's the kicker. It *seems* like the shared_from_this framework should give me what I want...
I can't solve your particular problem, but it seems to be that using a weak pointer is the wrong solution in your example code. The callback is a member of the control, and thus cannot outlive it. In other words, the weak pointer ought to be superfluous - use an ordinary pointer. If the callback can actually outlive the control, then it shouldn't be the control that creates it. External code should create the callback and pass it the shared_ptr to an existing control. Sebastian Redl
participants (1)
-
Sebastian Redl