Connecting Tech Pros Worldwide Help | Site Map

boost::shared_ptr and polymorphism

  #1  
Old September 28th, 2006, 05:45 PM
bb
Guest
 
Posts: n/a
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

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

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

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

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

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

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

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.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
templates and polymorphism mscava@gmail.com answers 8 February 20th, 2007 12:35 AM
Which libraries in Boost are mature enough to be used in real applications? Guch Wu answers 34 June 5th, 2006 12:45 PM
What is the advantage of Boost scoped_ptr? yinglcs@gmail.com answers 3 January 21st, 2006 12:55 PM
HELP: vector & polymorphism The Directive answers 4 July 22nd, 2005 05:32 AM