Abstract classes vs interfaces, casting from void*

John Colvin john.loughran.colvin at gmail.com
Fri Aug 9 12:26:59 UTC 2019


import std.stdio;

interface I
{
     void foo();
}

class C : I
{
     override void foo() { writeln("hi"); }
}

abstract class AC
{
     void foo();
}

class D : AC
{
     override void foo() { writeln("hi"); }
}

void main()
{
     auto c = new C();
     writeln(0);
     (cast(I)cast(void*)c).foo();
     writeln(1);
     (cast(C)cast(void*)c).foo();
     writeln(2);
     (cast(I)cast(C)cast(void*)c).foo();

     auto d = new D();
     writeln(3);
     (cast(AC)cast(void*)d).foo();
     writeln(4);
     (cast(D)cast(void*)d).foo();
     writeln(5);
     (cast(AC)cast(D)cast(void*)d).foo();
}

This produces the output:

0
1
hi
2
hi
3
hi
4
hi
5
hi

Why is there no "hi" between 0 and 1?


More information about the Digitalmars-d-learn mailing list