469,963 Members | 2,188 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

The practical use of dynamic_cast in reusable or generic code

I'm wondering about the practical use of dynamic_cast in reusable or
generic code.
I'm currently working on a smart pointer that can be used on vector
and other STL containers.
See following link:
http://code.axter.com/arr_ptr.h

In above code, there's a function called clone_func_ptr_interface
within the func_ptr_holder class.
In this function, I need to cast from a base pointer to a derived
pointer.
I originally used dynamic_cast, but then I realized it would force
anyone using the code to enable RTTI.

I was also worried that using dynamic_cast would make my code less
efficient.

Considering that most projects don't have RTTI enabled, and that
dynamic_cast could result in less efficient code, how practical is it
to use dynamic_cast in reusable or generic code?
Should it be avoided in this type of code, or should it be used
regardless of how less reusable it makes the code?

Jul 23 '05 #1
3 1818
In message <11*********************@o13g2000cwo.googlegroups. com>, Axter
<te**@axter.com> writes
I'm wondering about the practical use of dynamic_cast in reusable or
generic code.
I'm currently working on a smart pointer that can be used on vector
and other STL containers.
See following link:
http://code.axter.com/arr_ptr.h

In above code, there's a function called clone_func_ptr_interface
within the func_ptr_holder class.
In this function, I need to cast from a base pointer to a derived
pointer.
I originally used dynamic_cast, but then I realized it would force
anyone using the code to enable RTTI.
If it isn't enabled, virtual functions won't work and you no longer have
standard C++. If that's the case you are by definition off-topic here
:-(
I was also worried that using dynamic_cast would make my code less
efficient.
Did you measure it?

Considering that most projects don't have RTTI enabled,
???
and that
dynamic_cast could result in less efficient code, how practical is it
to use dynamic_cast in reusable or generic code?
dynamic_cast is provided because it does something none of the other
casts can do. If you need that functionality, never mind "practical",
it's *necessary*. If that's the case, any workaround is likely to be
even less efficient.
Should it be avoided in this type of code, or should it be used
regardless of how less reusable it makes the code?

Is it *needed*?

--
Richard Herring
Jul 23 '05 #2

Richard Herring wrote:
In message <11*********************@o13g2000cwo.googlegroups. com>, Axter <te**@axter.com> writes
I'm wondering about the practical use of dynamic_cast in reusable or
generic code.
I'm currently working on a smart pointer that can be used on vector
and other STL containers.
See following link:
http://code.axter.com/arr_ptr.h

In above code, there's a function called clone_func_ptr_interface
within the func_ptr_holder class.
In this function, I need to cast from a base pointer to a derived
pointer.
I originally used dynamic_cast, but then I realized it would force
anyone using the code to enable RTTI.
If it isn't enabled, virtual functions won't work and you no longer

have standard C++. If that's the case you are by definition off-topic here :-(
You don't need to have RTTI enable to have virtual functions working.
The virtual functions work without RTTI enabled.

I was also worried that using dynamic_cast would make my code less
efficient.


Did you measure it?

Considering that most projects don't have RTTI enabled,


???
and that
dynamic_cast could result in less efficient code, how practical is itto use dynamic_cast in reusable or generic code?


dynamic_cast is provided because it does something none of the other
casts can do. If you need that functionality, never mind "practical",

it's *necessary*. If that's the case, any workaround is likely to be
even less efficient.
Should it be avoided in this type of code, or should it be used
regardless of how less reusable it makes the code?

Is it *needed*?

--
Richard Herring


The point is, that the code can work without dynamic_cast, but
dynamic_cast could add extra runtime type safety.

So what I'm weighing is the difference between adding extra runtime
type safety and loosing efficiency as well as loosing reuse.

Jul 23 '05 #3
In message <11**********************@o13g2000cwo.googlegroups .com>,
Axter <te**@axter.com> writes

Richard Herring wrote:
In message <11*********************@o13g2000cwo.googlegroups. com>,Axter
<te**@axter.com> writes
>I'm wondering about the practical use of dynamic_cast in reusable or
>generic code.
>I'm currently working on a smart pointer that can be used on vector
>and other STL containers.
>See following link:
>http://code.axter.com/arr_ptr.h
>
>In above code, there's a function called clone_func_ptr_interface
>within the func_ptr_holder class.
>In this function, I need to cast from a base pointer to a derived
>pointer.
>I originally used dynamic_cast, but then I realized it would force
>anyone using the code to enable RTTI.


If it isn't enabled, virtual functions won't work and you no longer

have
standard C++. If that's the case you are by definition off-topic here

:-(


You don't need to have RTTI enable to have virtual functions working.
The virtual functions work without RTTI enabled.


Then you must mean something different from me by "RTTI". It doesn't
matter, anyway. I'll rephrase the above.

If dynamic_cast isn't enabled, you no longer have standard C++. If
that's the case you are by definition off-topic here.
>I was also worried that using dynamic_cast would make my code less
>efficient.


Did you measure it?
>
>Considering that most projects don't have RTTI enabled,


???
>and that
>dynamic_cast could result in less efficient code, how practical isit >to use dynamic_cast in reusable or generic code?


dynamic_cast is provided because it does something none of the other
casts can do. If you need that functionality, never mind "practical",

it's *necessary*. If that's the case, any workaround is likely to be
even less efficient.
>Should it be avoided in this type of code, or should it be used
>regardless of how less reusable it makes the code?
>

Is it *needed*?


The point is, that the code can work without dynamic_cast, but
dynamic_cast could add extra runtime type safety.

So what I'm weighing is the difference between adding extra runtime
type safety and loosing efficiency


Then _weigh_ it. So far you're just _talking_ about it. Have you
actually measured the efficiency loss?
as well as loosing reuse.


Standard C++ provides dynamic_cast, so that's not a genuine issue.

--
Richard Herring
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tony Marston | last post: by
10 posts views Thread by richardclay09 | last post: by
3 posts views Thread by Armen Rizal | last post: by
15 posts views Thread by Grizlyk | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.