interface ABI

mpt foo at bar.com
Wed Jan 31 14:33:30 PST 2007


I wrote a scripting system where the virtual machine relies on storing
objects in void* pointers. The problem is that D handles interfaces and
classes differently. The code below prints different addresses (8 byte
offset) when Base is an interface, but same addresses when it is a class.
So my question is, why are they different like this?

Another thing I found is that when Base is an interface, the resulting
machine code is 5 operations longer (and slower). The extra code checks if
the cast object is null and calculates the offset.

import std.stdio;

interface Base {
    void Func();
}

class Foo : Base {
    void Func() {}
}

void main( char[][] args ) {
    Foo f = new Foo();
    Base b = f;

    writefln( cast(void*)f );
    writefln( cast(void*)b );

    f.Func();
    b.Func();
}



More information about the Digitalmars-d mailing list