 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
David Baraff Guest
|
Posted: Fri Feb 27, 2004 4:35 am Post subject: 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.
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Fri Feb 27, 2004 4:47 am Post subject: Re: can __dynamic_cast() be used to perform a trivial cast? |
|
|
* [email]deb (AT) pixar (DOT) com[/email] (David Baraff) schriebt:
| Quote: |
I'm playing around with the __cxxabiv1::__dynamic_cast() function.
|
Heh heh, it's the function from Charon!
| Quote: | 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.
| Quote: | with one exception.
|
Oh?
| Quote: | Suppose that Base and Derived are polymorphic, with Base a public base
class of Derived.
|
Yep yep yep, get on to it!
| Quote: | 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).
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|