trivial __dynamic_cast fails?

David Baraff deb at pixar.com
Fri Feb 27 15:56:07 UTC 2004


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.




More information about the cxx-abi-dev mailing list