|
Boost : |
From: Jonathan Wakely (cow_at_[hidden])
Date: 2004-12-03 11:10:07
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk