Address of instance member function

Deewiant deewiant.doesnotlike.spam at gmail.com
Sat Mar 31 10:19:25 PDT 2007


Jarrett Billingsley wrote:
 > It's a function pointer to that method.  It's actually useful -- you can
> simulate pointer-to-members using this and delegates:
> 
> class A
> {
>     int mX;
> 
>     this(int x)
>     {
>         mX = x;
>     }
> 
>     void foo(int y)
>     {
>         writefln(mX, ", ", y);
>     }
> }
> 
> void main()
> {
>     scope a = new A(5);
>     a.foo(4);
> 
>     void delegate(int) dg;
>     dg.funcptr = &A.foo; // <<- dah
>     dg.ptr = cast(void*)a;

Are there other uses? Doesn't the above essentially boil down to just:

auto dg = &a.foo;

>     dg(8);
> }
> 

-- 
Remove ".doesnotlike.spam" from the mail address.


More information about the Digitalmars-d-learn mailing list