473,324 Members | 2,531 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,324 software developers and data experts.

can __dynamic_cast() be used to perform a trivial cast?

I'm playing around with the __cxxabiv1::__dynamic_cast() function. It
works as I would expect, with one exception.

Suppose that Base and Derived are polymorphic, with Base a public base
class of Derived.
Given
Derived* d = new Derived;
Base* b = d;

I would like to be able to do

void* result = __dynamic_cast(d, &typeid(Derived), &typeid(Base),
-1);

which means, literally, "try to cast this pointer of type Derived to
Base." (And yes I have a good reason for wanting to do something that
appears that trivial. This is in a context where all you will have to
work with are void*'s and type_info structures. So the fact that you
are going in the "easy" direction for the cast doesn't help you any.).

Unfortunately, I always get NULL back -- it seems like the
__dynamic_cast() operator doesn't want to cast from a Derived to a
Base. Am I doing anything wrong? I can go the other direction just
fine. I can't find any other functions in the API that'll do what I
want.

---------------
* I didn't really pass in &typeid(Derived), I actually pass in a
dynamic_cast<const __class_type_info*>(&typeid(Derived))
and similarly for Base.
Jul 22 '05 #1
1 2168
* de*@pixar.com (David Baraff) schriebt:

I'm playing around with the __cxxabiv1::__dynamic_cast() function.
Heh heh, it's the function from Charon!
It works as I would expect,
Good to hear, but what exactly is it you expect?

I don't know, and suspect few readers (if any) of this group know.

You see, the Holy Standard does not mention any Charon-functions.
with one exception.
Oh?
Suppose that Base and Derived are polymorphic, with Base a public base
class of Derived.
Yep yep yep, get on to it!
Given
Derived* d = new Derived;
Base* b = d;

I would like to be able to do

void* result = __dynamic_cast(d, &typeid(Derived), &typeid(Base),
-1);

which means, literally, "try to cast this pointer of type Derived to
Base."


That's easy to accomplish using Holy Standard functionality, to wit:
void* result = dynamic_cast<Base*>( d );
but a bit more difficult if Derived and Base are only dynamically known
through their std::type_info structures.

In the latter case you don't have enough information.

You then need something more powerful than std::type_info, in effect, your
own type identification scheme with support for casting here and there.

But it will probably be much easier to scratch the current design.

And keep the knowledge (of what to do, and what _not_ to do).

Jul 22 '05 #2

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

Similar topics

15
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ...
36
by: MSG | last post by:
The answer is neither. Use macros. #define ALLOC(size, type) ((type) *) malloc((size) * sizeof(type)) #define NEW(type, name, size) (type) * (name) = ALLOC((size), (type)) They are both ...
1
by: Ben Harper | last post by:
I define an interface in managed C++: I call it ISgPlugin. I implement that interface in a C# dll: Using reflection, I try to load and instantiate that interface from the C++ module. I get far,...
1
by: Rich S. | last post by:
Hello, I need to interact with the Win32 API "BackupRead," and it returns a byte array as one of its parameters. I need to be able to cast the first part of the byte array into a special...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
5
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
2
by: syntego | last post by:
We commonly use triggers to log changes to our main tables to historical log tables. In the trigger, we create a concatenated string of the old values by casting them as follows: ...
11
by: James R. Davis | last post by:
Yes, a newbie here. Though I am making progress, slowly, I am also getting more and more confused. With ASP, when I wanted to do something as trivial as updating a visitor counter, I...
3
by: NorseMN | last post by:
I suppose my problem really started when I decided to implement a top-down design, because management always wants to see the frosting before they let you bake a cake. In any case, I now seem to have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.