casting smart pointer

Hi, how can I upcast a shared_ptr? class Base { /* ... */ } class Derived : public Base { /* ... */ } shared_ptr<Base> Get( const char* pName ); shared_ptr<Derived> spDerived = *_cast<*>( Get( "Derived" ) ); // What do I need to write instead the * ? Depending on the passed name I like to upcast the returned pointer to a pointer to the derived class. I used a static_cast with raw pointers before. What do I have to use with shared_ptr? Do I need to enable RTTI? Regards, -Dirk

On Fri, Dec 03, 2004 at 04:59:10PM +0100, Dirk Gregorius wrote:
Hi,
how can I upcast a shared_ptr?
class Base { /* ... */ } class Derived : public Base { /* ... */ }
shared_ptr<Base> Get( const char* pName );
shared_ptr<Derived> spDerived = *_cast<*>( Get( "Derived" ) ); // What do I need to write instead the * ?
That's a downcast, isn't it? You want static_pointer_cast<Derived>( Get("Derived") ); Defined in the same header as shared_ptr.
Depending on the passed name I like to upcast the returned pointer to a pointer to the derived class. I used a static_cast with raw pointers before. What do I have to use with shared_ptr? Do I need to enable RTTI?
Only if you want to use dynamic_pointer_cast. jon -- "The value of a technical conversation is inversely proportional to how well the participants are dressed." - Larry McVoy
participants (2)
-
Dirk Gregorius
-
Jonathan Wakely