Boost logo

Boost Users :

Subject: Re: [Boost-users] casting shared pointers to derived classes
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2012-01-11 11:23:21


On Wed, Jan 11, 2012 at 4:18 PM, Don Wilde <donintel11_at_[hidden]> wrote:

> Greetings, all -
>
> I am updating a neural network package from the DOS days to modern C++
> standards.
>
> I have a shared_ptr to a vector of shared_ptrs of type Layer. This class
> has derived classes OutputLayer, MiddleLayer and InputLayer. Each of these
> has their own method for calculating output updates, so I need to be able
> to cast. from boost::shared_ptr<Layer> to static_cast<OutputLayer*>().
>
> Unfortunately, VC++ 2010 will not allow me to do so. It won't even allow a
> reinterpret_cast. I've looked on the Web and I also have Schalling's guide
> to Boost.
>

 Not only VC++ does not allow this, but the C++ standard as well.

> Thank you in advance for your guidance. I'm using boost_1_47_0, if that
> makes a difference. :D
>

Use tools provided by the shared_ptr itself...

Take a look at
http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/shared_ptr.htm#Synopsis,
especially at these functions:

 template<class T, class U>
    shared_ptr<T> static_pointer_cast
<http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/shared_ptr.htm#static_pointer_cast>(shared_ptr<U>
const & r); // never throws

  template<class T, class U>
    shared_ptr<T> const_pointer_cast
<http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/shared_ptr.htm#const_pointer_cast>(shared_ptr<U>
const & r); // never throws

  template<class T, class U>
    shared_ptr<T> dynamic_pointer_cast
<http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/shared_ptr.htm#dynamic_pointer_cast>(shared_ptr<U>
const & r); // never throws

Greetings,
Ovanes



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net