Is it safe to use such smart pointer casting?
APtr a(new A());
BPtr & b = (Bptr&)a; // this is it
there,
class A
{
public:
virtual ~A(){}
virtual void methodA() = 0;
}
typedef std::tr1::shared_ptr<A> APtr;
class B : public A
{
public:
virtual ~B(){}
virtual void methodB() = 0;
}
typedef std::tr1::shared_ptr<B> BPtr;
/////////////////////////////////////////////////////////////////////////////////
BPtr & b = a; //this way doesn't work
To downcast a smart pointer, you should use the xxxx_pointer_cast functions, e.g. a static cast
BPtr b = std::tr1::static_pointer_cast<B>(a);
or dynamic cast
BPtr b = std::tr1::dynamic_pointer_cast<B>(a);