Problem with casting instance reference to void* and back.

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 27 06:16:01 PDT 2015


On Monday, 27 July 2015 at 13:11:33 UTC, anonymous wrote:
> In the first example, you pass a pointer to a class instance. 
> You cannot get the vtbl entry for the interface like this. 
> Instead try to do this in 2 steps:

actually i meant you pass an untyped pointer, so when you cast as 
interface it's not like if the vtbl of an object is queried. So 
you have to upcast to an Object then cast to the interface, which 
is not a simple cast (it get something in the virtual table.

void testDispathcer(void *rawSelf) {
     auto obj = cast(Object) rawSelf; // reinterpret
     auto self = cast(TestInterface) obj; // get vtbl entry , it's 
not a simple cast
     // works
     self.testMethod();
}




More information about the Digitalmars-d-learn mailing list