Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 28th, 2006, 05:45 PM
bb
Guest
 
Posts: n/a
Default boost::shared_ptr and polymorphism

Hi,

Is boost::shared_ptr polymorphic?

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

Thanks.

  #2  
Old September 28th, 2006, 06:05 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: boost::shared_ptr and polymorphism

bb wrote:
Quote:
Is boost::shared_ptr polymorphic?
>
i.e., can i do safe downcasting using dynamic_cast?
Why ask? Why not just try and see FCOL? :-)


  #3  
Old September 28th, 2006, 06:35 PM
Pete Becker
Guest
 
Posts: n/a
Default Re: boost::shared_ptr and polymorphism

bb wrote:
Quote:
>
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.
  #4  
Old September 28th, 2006, 10:15 PM
Roland Pibinger
Guest
 
Posts: n/a
Default Re: boost::shared_ptr and polymorphism

On 28 Sep 2006 09:59:23 -0700, "bb" <muralibala68@gmail.comwrote:
Quote:
>
>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??
  #5  
Old September 29th, 2006, 12:05 AM
Jens Theisen
Guest
 
Posts: n/a
Default Re: boost::shared_ptr and polymorphism

rpbg123@yahoo.com (Roland Pibinger) writes:
Quote:
On 28 Sep 2006 09:59:23 -0700, "bb" <muralibala68@gmail.comwrote:
Quote:

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
  #6  
Old September 29th, 2006, 06:35 AM
Roland Pibinger
Guest
 
Posts: n/a
Default Re: boost::shared_ptr and polymorphism

On 29 Sep 2006 00:14:42 +0100, Jens Theisen <jth02@arcor.dewrote:
Quote:
>rpbg123@yahoo.com (Roland Pibinger) writes:
Quote:
>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.
Quote:
>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
  #7  
Old September 29th, 2006, 07:15 AM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default Re: boost::shared_ptr and polymorphism

Roland Pibinger wrote:
Quote:
On 29 Sep 2006 00:14:42 +0100, Jens Theisen <jth02@arcor.dewrote:
Quote:
>>rpbg123@yahoo.com (Roland Pibinger) writes:
Quote:
>>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.
>
Quote:
>>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
  #8  
Old October 6th, 2006, 11:35 AM
bb
Guest
 
Posts: n/a
Default Re: boost::shared_ptr and polymorphism

Thanks Peter.

Pete Becker wrote:
Quote:
bb wrote:
Quote:

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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles