473,387 Members | 1,641 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,387 software developers and data experts.

Replacing dynamic_cast

Hi everybody,
I'm working in a system that doesn't provide rtti functionality, so using
dynamic_cast is not
possible. I'm wondering how would be a better replacement: using static or
reintepret cast?
(Or something else?). This is to modify an existing program, so please note
that a redesign
is not possible.

Thanks!
Jul 23 '05 #1
3 2640

"notme" <a@b.c> wrote in message news:3a*************@individual.net...
Hi everybody,
I'm working in a system that doesn't provide rtti functionality, so using
dynamic_cast is not
possible. I'm wondering how would be a better replacement: using static or
reintepret cast?
(Or something else?). This is to modify an existing program, so please
note that a redesign
is not possible.


I'm not sure what kind of "system" doesn't provide RTTI...? The
dynamic_cast can be used wherever you have a class hierarchy, to cast a
pointer (or reference) from one type to another type in the same hierarchy
tree (i.e., from a base* to a derived*, or the reverse). As far as I know,
any valid C++ compiler provides this ability.

Each of the casts is for a very specific purpose, and you simply can't just
choose to use a different one.

Perhaps a simple C-style cast would work? It's not preferred, and it's not
type-safe, but it works (in general).

In any case, without some idea of what you're trying to cast and why, it
would be hard for us to guess at a solution.

-Howard
Jul 23 '05 #2
"notme" <a@b.c> wrote in message news:3a*************@individual.net...
I'm working in a system that doesn't provide rtti functionality, so using
dynamic_cast is not
possible. I'm wondering how would be a better replacement: using static or
reintepret cast?
The need for dynamic_cast is rare. May be you can get away without using it.

If you have to downcast, use static_cast and only when you are sure that the
base pointer (or reference) really points to (or refers to) that derived
type.
(Or something else?). This is to modify an existing program, so please note that a redesign
is not possible.


A replacement for dynamic_cast is using the visitor pattern. I don't know
whether you can incorporate that into the existing design.

Ali

Jul 23 '05 #3
notme wrote:
...
I'm working in a system that doesn't provide rtti functionality, so using
dynamic_cast is not
possible. I'm wondering how would be a better replacement: using static or
reintepret cast?
(Or something else?). This is to modify an existing program, so please note
that a redesign
is not possible.
...


That would be 'static_cast', of course. The functionality of
'reinterpret_cast' is not even close to that of 'dynamic_cast'. But keep
in mind that 'static_cast' does not implement all the conversions that
can be performed by 'dynamic_cast' (for example, it cannot perform the
proper conversion to 'void*'), so be careful with the switch.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
3
by: Axter | last post by:
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...
1
by: Steven T. Hatton | last post by:
The result shown below doesn't surprise me now. But it did several months ago when I followed some bad advice and tried to check if I had a live object at the address referenced by a pointer. Can...
3
by: Ganesh | last post by:
On devx site, I saw following code. It says when a derived class is tried to cast to base type, it looks at the missing vtable and complains if the object is already deleted. I am of the opinion...
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 ...
5
by: Jim Langston | last post by:
I am using some code that I got that uses a form a message dispatching where the data is passed via a void*. I don't like void*'s so am experimenting with a different way to do them in C++. I...
25
by: lovecreatesbea... | last post by:
Suppose I have the following three classes, GrandBase <-- Base <-- Child <-- GrandChild The following cast expression holds true only if pBase points object of type of ``Child'' or...
18
by: Eric | last post by:
Ok...this seems to be treading into some really esoteric areas of c++. I will do my best to explain this issue, but I don't fully understand what is happening myself. I am hoping that the problem...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...

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.