virtual method pointer

Timon Gehr timon.gehr at gmx.ch
Thu May 3 12:11:14 PDT 2012


On 05/03/2012 09:01 PM, Gor Gyolchanyan wrote:
> class B: A
> {
>      void foo()
>      {
>          writeln("B.foo called");
>      }
> }
>
> void main()
> {
>     auto a = new A();
>     auto fn =&a.foo;
>     auto ptr = fn.funcptr;
>     auto b = new B();
>     (cast(void function(A))ptr)(b);
> }
>
> will this work?
>

It should work (at least) with the D calling convention, yes.

A somewhat more portable way would probably be:

auto fn = &a.foo;
fn.ptr = cast(void*)b;
fn();


More information about the Digitalmars-d mailing list