Boost logo

Boost :

From: belopols_at_[hidden]
Date: 2001-04-11 03:43:25


Is it possible to downcast boost::shared_ptr ?

Suppose I have a container of polymorphic objects:

std::vector<boost::shared_ptr<Object> > c;
c.push_back(boost::shared_ptr<Derived>(new Derived));

I can retrieve the object and downcast it:

if (Derived* pd = dynamic_cast<Derived*>(c.front().get()))
{
    pd->someMethodOfDerivedClass();
}

Now suppose that I want to call a function which expects
boost::shared_ptr<Derived>. I cannot simply create
boost::shared_ptr<Derived>(p) and pass it in, since
reference count will be wrong. What I need here is
a downcast template which I can use as follows

boost::shared_ptr<Derived> spd =

    downcast<boost::shared_ptr<Derived> >(c.front());

However I don't see how to implement it using public
interface to boost::shared_ptr only.

--
Alexander Belopolsky 

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk