B
bb
Hi,
Is boost::shared_ptr polymorphic?
i.e., can i do safe downcasting using dynamic_cast?
Thanks.
Is boost::shared_ptr polymorphic?
i.e., can i do safe downcasting using dynamic_cast?
Thanks.
bb said:Is boost::shared_ptr polymorphic?
i.e., can i do safe downcasting using dynamic_cast?
bb said:Is boost::shared_ptr polymorphic?
i.e., can i do safe downcasting using dynamic_cast?
Is boost::shared_ptr polymorphic?
i.e., can i do safe downcasting using dynamic_cast?
How do you come to the idea that 'smart' pointers are pointers??
That's not helpful. You don't like smart pointers,
so you're taking a
beginners question to express it by putting smart in quotes and
implying that they are not proper pointers. What's the point of this?
Roland said:IT's not a question of like and dislike.
The point is that smart pointers are classes/templates which mimic
pointers by overloading operator* but which are, of course, not 'real'
pointers (as definded in the C/C++ language specifications) and also
no replacement for 'real' pointers. Ignoring the difference leads to
surprises (see eg. above). BTW, this was discussed years ago in an
article that generated some attention:
http://www-sor.inria.fr/publi/SPC++_usenixC++92.html.
Pete said:Almost. dynamic_cast itself won't work, because the compiler doesn't now
about the internals of shared_ptr. But you can use dymamic_pointer_cast,
which applies dynamic_cast to the stored pointer:
#include <memory>
std::tr1::shared_ptr<Base> sp0(new Derived);
std::tr1::shared_ptr<Derived> sp1 = dynamic_pointer_cast<Derived>(sp0);
For more details, see section 2.8.3 of my book, "The Standard C++
Library Extensions." (std::tr1::shared_ptr is pretty much the same as
boost::shared_ptr, which it was based on)
--
-- Pete
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.