Replacing dynamic_cast

N

notme

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!
 
H

Howard

notme said:
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
 
G

Guest

notme said:
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
 
A

Andrey Tarasevich

notme said:
...
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top