On 14.10.2015 15:26, Szymon Gatner wrote:
To get compatible class layout, the D compiler has to omit it's class
info entry in the vtable of C++ classes. In addition D doesn't know
about C++ RTTI (I don't know if this is planned to add), so it cannot
do the dynamic cast from Operation to Subtract.
I have not tried, but calling the virtual destructor instead of
destroy() might just work?
Mint that this code comes from Adam Ruppe's book, and it is suppose to
be working code. Also mind that Subtract is D class and instantiated on
D side, it is only briefly passed to C++ for usage.
Check the disassembly: the compiler is not even trying to do a
conversion, it just loads null. There is no usable type information in a
If you know the object "animal" is of type Subtract, you can get the
object pointer by manually subtracting the pointer difference from the
auto ptr = cast(void*)animal;
ptr -= cast(void*)cast(Operation)cast(Subtract)ptr - ptr;
auto cat = cast(Subtract) ptr;