reimplementing an interface in a derived class

Neia Neutuladh neia at ikeran.org
Fri Jan 4 21:47:59 UTC 2019


On Fri, 04 Jan 2019 08:46:24 +0000, Alex wrote:
> Let's assume this is right. How to force a B object to behave like an A
> object? I thought casting is a possible approach...

It requires a bit of surgery:

    import std.stdio;
    class A
    {
        void foo() { writeln("hello from A!"); }
    }
    class B : A
    {
        override void foo() { writeln("hello from B!"); }
    }
    void main()
    {
        auto b = new B;
        auto ptrB = cast(void**)b;
        ptrB[0] = A.classinfo.vtbl.ptr;
        b.foo();
    }

This takes advantage of the object layout used by DMD. 'vtbl' is the 
virtual function table, which is basically an array of function pointers. 
Each member function defined in a type (and its super types) gets a unique 
index into that array.

So when you write:

    b.foo();

That works out to:

    (cast(void function(B))b.vtbl[5])(b);

We replace object b's vtbl with class A's, and now b is just an A with 
some extra stuff allocated in its memory block.

Don't do this in production code. This is a terrible thing to do in 
production code, or any code you intend to use other than to see how D's 
object model works.


More information about the Digitalmars-d-learn mailing list