[Boost-users] casting shared pointers to derived classes

6 views
Skip to first unread message

Don Wilde

unread,
Jan 11, 2012, 10:18:55 AM1/11/12
to boost...@lists.boost.org
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.
 
Thank you in advance for your guidance. I'm using boost_1_47_0, if that makes a difference. :D

spam-r...@web.de

unread,
Jan 11, 2012, 11:17:50 AM1/11/12
to boost...@lists.boost.org
Cast a Layer to an OutputLayer:

boost::shared_ptr<Layer> layer = getLayer();
boost::shared_ptr<OutputLayer> outputLayer =
boost::dynamic_pointer_cast<OutputLayer>(layer);

But please consider to declare your calculation method virtual and to
provide different implementations in your derived classes. You would not
have to cast at all then.

Thomas

-------- Original Message --------
Subject: [Boost-users] casting shared pointers to derived classes
From: Don Wilde <donin...@yahoo.com>
To: boost...@lists.boost.org <boost...@lists.boost.org>
Date: Wed Jan 11 2012 16:18:55 GMT+0100

> 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<*>().


> 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.
> Thank you in advance for your guidance. I'm using boost_1_47_0, if that
> makes a difference. :D
>
>

> _______________________________________________
> Boost-users mailing list
> Boost...@lists.boost.org
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Ovanes Markarian

unread,
Jan 11, 2012, 11:23:21 AM1/11/12
to boost...@lists.boost.org
On Wed, Jan 11, 2012 at 4:18 PM, Don Wilde <donin...@yahoo.com> 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...


 template<class T, class U>
    shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r); // never throws

  template<class T, class U>
    shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r); // never throws

  template<class T, class U>
    shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r); // never throws
 
Greetings,
Ovanes

Don Wilde

unread,
Jan 11, 2012, 11:58:35 AM1/11/12
to boost...@lists.boost.org
[snip]
 
Use tools provided by the shared_ptr itself...
 template<class T, class U>
    shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r); // never throws

  template<class T, class U>
    shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r); // never throws

  template<class T, class U>
    shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r); // never throws
 [snip]
 
Thank you Ovanes and Thomas for your insights!
 
I appreciate the help. :D

Václav Zeman

unread,
Jan 11, 2012, 12:08:17 PM1/11/12
to boost...@lists.boost.org
On 01/11/2012 05:17 PM, spam-r...@web.de wrote:
> Cast a Layer to an OutputLayer:
>
> boost::shared_ptr<Layer> layer = getLayer();
> boost::shared_ptr<OutputLayer> outputLayer =
> boost::dynamic_pointer_cast<OutputLayer>(layer);
I wonder why is boost::detail::dynamic_cast_tag part of the detail
namespace and not part of the public API instead. It seems it would be
useful in this and similar cases.

>[...]

--
VZ

signature.asc
Reply all
Reply to author
Forward
0 new messages