473,473 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1977
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.