ClassInfo, derived types, and object.Interface

Kapps opantm2+spam at gmail.com
Sun Aug 25 14:16:34 PDT 2013


I'm trying to implement a runtime reflection module, and am 
having some issues with figuring out the function pointer to 
invoke an interface method on a class that derives from the one 
that implements the interface.

For example, given the following code, we can call an interface 
function using a delegate: http://pastie.org/8255612 - but even 
with the derived instance passed in it will invoke Bar's 
implementation as we're using Bar's object.Interface instance and 
thus vtable. I figured typeid(DerivedBar).interfaces would
return it's own Interface instance for DerivedBar, but it returns 
an empty array and instead Bar's has to be used.

The ABI page doesn't seem to mention much about inheriting from a 
class that implements an interface and overrides it's functions. 
Is there a way to get the function pointer to the overridden 
version of foo on DerivedBar using Bar's object.Interface 
instance for Foo? How does the compiler know what method to call 
in this situation?

I'm hoping to accomplish the below without storing data for 
DerivedBar:
auto metadata = createMetadata!Foo;
DerivedBar instance = ... // DerivedBar is not known at 
compile-time.
metadata.invokeMethod("bar", instance, params);


More information about the Digitalmars-d mailing list