boost::shared_ptr and polymorphism

B

bb

Hi,

Is boost::shared_ptr polymorphic?

i.e., can i do safe downcasting using dynamic_cast?

Thanks.
 
P

Pete Becker

bb said:
Is boost::shared_ptr polymorphic?

i.e., can i do safe downcasting using dynamic_cast?

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.
 
R

Roland Pibinger

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??
 
J

Jens Theisen

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?

Besides, I don't see any connection between your mockery and the
original question, given that shared_ptr does provide a means for
doing what the OP wants, namely static_pointer_cast and
dynamic_pointer_cast.

Regards,

Jens
 
R

Roland Pibinger

That's not helpful. You don't like smart pointers,

IT's not a question of like and dislike.
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?

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.

Best wishes,
Roland Pibinger
 
K

Kai-Uwe Bux

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.

Hm, if your position is that smart pointers are not pointers, then I wonder
why you wrote

'smart' pointers

(insinuating they ain't smart) instead of

smart 'pointers'

(hinting at them not being pointers). Maybe, to a certain degree this is a
matter of likes and dislikes.


Best

Kai-Uwe Bux
 
B

bb

Thanks Peter.

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.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top