By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

boost::shared_ptr and polymorphism

P: n/a
bb
Hi,

Is boost::shared_ptr polymorphic?

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

Thanks.

Sep 28 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
bb wrote:
Is boost::shared_ptr polymorphic?

i.e., can i do safe downcasting using dynamic_cast?
Why ask? Why not just try and see FCOL? :-)
Sep 28 '06 #2

P: n/a
bb wrote:
>
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<Basesp0(new Derived);
std::tr1::shared_ptr<Derivedsp1 = 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.
Sep 28 '06 #3

P: n/a
On 28 Sep 2006 09:59:23 -0700, "bb" <mu**********@gmail.comwrote:
>
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??
Sep 28 '06 #4

P: n/a
rp*****@yahoo.com (Roland Pibinger) writes:
On 28 Sep 2006 09:59:23 -0700, "bb" <mu**********@gmail.comwrote:

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?

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
Sep 28 '06 #5

P: n/a
On 29 Sep 2006 00:14:42 +0100, Jens Theisen <jt***@arcor.dewrote:
>rp*****@yahoo.com (Roland Pibinger) writes:
>How do you come to the idea that 'smart' pointers are pointers??

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
Sep 29 '06 #6

P: n/a
Roland Pibinger wrote:
On 29 Sep 2006 00:14:42 +0100, Jens Theisen <jt***@arcor.dewrote:
>>rp*****@yahoo.com (Roland Pibinger) writes:
>>How do you come to the idea that 'smart' pointers are pointers??

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.
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
Sep 29 '06 #7

P: n/a
bb
Thanks Peter.

Pete Becker wrote:
bb wrote:

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<Basesp0(new Derived);
std::tr1::shared_ptr<Derivedsp1 = 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.
Oct 6 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.