Boost logo

Boost Users :

From: Vaclav Vesely (vaclav.vesely_at_[hidden])
Date: 2006-03-24 09:10:19


Dhanvi Kapila wrote:
> I have a boost::shared_ptr that stores a pointer to a base class (
> class B1 ) and derived classes ( class D1 , class D2 ) both inheriting
> from the same Base class.
> The Constructors
> I have another set of factory classes ( base class : class base1,
> derived class : class derv1 & class derv2 ) whoose constructors accepts
> boost::shared_ptr of B1.
>
> When constructing derv1: I would like to cast pointer as D1 and ,when
> constructing derv2 : cast pointer as D2, but the only way I can access
> the class functions is through a shared_ptr to B1.
>
> My question is how do I cast B1 to get D1 and D2 in the constructors for
> derv1 and derv2 respectively ?
>
> base1* createBase( boost::shared_ptr<B1> b_ptr , int type )
> {
>
> if( type == 1 )
> return new derv1( boost::polymorphic_downcast<AAAA> b_ptr );
> else
> return new derv2( boost::polymorphic_downcast<XXXX> b_ptr );
> }
>
> what do i write in the place of AAAA & XXXX for the code to wrk ??

What about

        return new derv1( boost::dynamic_pointer_cast<B1>(b_ptr));

see http://www.boost.org/libs/smart_ptr/shared_ptr.htm#dynamic_pointer_cast

Regards,
Vaclav


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