typecasting a boost::shared_ptr ?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

typecasting a boost::shared_ptr ?

Dhanvi Kapila-2
Hi Friends,
 
 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 ?

the pseudo code is

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 ??

Sorry for the long question, but could not express it in any shorter way..

thanks for ur patience and replies

Dhanvi
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: typecasting a boost::shared_ptr ?

Vaclav Vesely
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 mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users