472,358 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 1906
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tony Marston | last post by:
For those of you who think that using XSL/XML to create your HTML output from a PHP application is not a viable proposition as XSL is a clunky language, it is too verbose and it cannot get the job...
1
by: Rhett | last post by:
Hello,EveryBody! I'm troubled by a design problem, Hoping that you'll save me out! The Situation is: We have two project of code A and B.A is our reusable code base, B is for specified for...
13
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when...
10
by: richardclay09 | last post by:
Please take a look at this method: template<class C> void f(C* ptrAny) { Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny); if(ptrFruit) { // do something specific to fruits } // Carry on using...
5
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to...
3
by: Armen Rizal | last post by:
Hello all, Is there anybody know where I can find reusable pl/pgsql samples or function library ? Thanks, Armen
22
by: Boris | last post by:
I'm porting code from Windows to UNIX and ran into a problem with dynamic_cast. Imagine a class hierarchy with three levels: class Level2 derives from Level1 which derives from Base. If you look...
15
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to ...
6
by: Generic Usenet Account | last post by:
I ran a small experiment involving RTTI and dynamic casting. Basically what I did was to cast a base class pointer to a derived type (yes, I know that is not kosher). I then performed...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.