Address of instance member function

Max Samukha samukha at voliacable.com
Sat Mar 31 09:48:46 PDT 2007


On Sat, 31 Mar 2007 11:43:47 -0400, "Jarrett Billingsley"
<kb3ctd2 at yahoo.com> wrote:

>"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);
>}
>
>
Thanks a lot. That's exactly what I need


More information about the Digitalmars-d-learn mailing list