shared_ptr<T>
has an interesting overloaded constructor:
template<class Y> shared_ptr(shared_ptr<Y> const & r, element_type * p);
Basically this constructs a shared_ptr which takes the deleter and the refcounting from r
, except that it holds p
.
You can use it like this:
TypedClass<int>::Ptr int_object = TypedClass<int>::Ptr(new TypedClass<int>());
TypedClass<float>::Ptr float_object(int_object,reinterpret_cast<TypedClass<float>*>(int_object.get()));
EDIT:
If you are using Boost >= 1.53.0, there is also boost::reinterpret_pointer_cast
. So you can write:
TypedClass<float>::Ptr float_object = boost::reinterpret_pointer_cast<TypedClass<float> >(int_object);