Address of instance member function

Jarrett Billingsley kb3ctd2 at yahoo.com
Sat Mar 31 08:43:47 PDT 2007


"Max Samukha" <samukha at voliacable.com> wrote in message 
news:11js039sich4vjo364at18so5m625bdvc8 at 4ax.com...
> What is the meaning of the value returned by the address operator
> applied to an instance member function as if that function were
> static?
>
> class Foo
> {
> void bar()
> {
> }
> }
>
> void main()
> {
> writefln(&Foo.bar);
> }

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;
    dg(8);
}





More information about the Digitalmars-d-learn mailing list