How to dispatch a class function for an object accessed by handle?

Steven Schveighoffer schveiguy at gmail.com
Thu Mar 5 14:46:24 UTC 2020


On 3/5/20 9:24 AM, wjoe wrote:

> 
> but how can I call fn in the context of an object instance?

You could do it with delegates. But it's ugly:

import std.stdio;
class C
{
     void foo() { writeln("Yup");}
}
void main()
{
     alias f = C.foo;
     auto c = new C;
     void delegate() dg;
     dg.funcptr = &f;
     dg.ptr = cast(void*)c;
     dg(); // prints "Yup"
}

I don't know of a way to call f with c aside from this.

-Steve


More information about the Digitalmars-d-learn mailing list