473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

boost::shared_ptr and polymorphism

bb
Hi,

Is boost::shared_ptr polymorphic?

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

Thanks.

Sep 28 '06 #1
7 12421
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Philippe Guglielmetti | last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with something like: class A; // forward typedef boost::smart_ptr<A> Aptr; class B{ Aptr a; virtual ~B(); // implemented...
5
by: ctick | last post by:
Are there any advantages of using boost::shared_ptr other than auto_ptr from standard library?
6
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
2
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include...
2
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
1
by: rssmps | last post by:
Hi, first post and here it goes... This is the current code (working) that I have. It works but for large number of A & B objects, it's not the most efficient. It's also not really heterogenous...
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
10
by: tradevol | last post by:
Hi, I am playing with boost pointer and try to wrap the following codes A* func(){ ... if(condition 1 ){ return a; } else
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.