Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-10-22 07:11:46


sashan wrote:
>> virtual void f()
>> {
>> shared_ptr<A> pA(shared_from_this());
>> shared_ptr<B> pB(dynamic_pointer_cast< shared_ptr<B> >(pA));
>> }
>> };
>>
> I just realized if I replaced
>
> shared_ptr<B> pB(dynamic_pointer_cast< shared_ptr<B> >(pA));
>
> with
>
> shared_ptr<B> pB(dynamic_pointer_cast< B >(pA));
>
>
> It will compile. What I'd like to know is if this is the way to do it.

In short, yes. The *_pointer_cast<> functions take the pointee type as a
template parameter and return a pointer of the same category as the
argument.

You may want to consider making A an abstract base class with f() being pure
virtual and only adding enable_shared_from_this<> to B. This isn't always
possible, but when it is, it's often a clearer design.


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