Boost logo

Boost :

From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2006-09-20 13:31:06

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

Boost list run by bdawes at, gregod at, cpdaniel at, john at