Pointers to non-static member functions!

Steven Schveighoffer schveiguy at yahoo.com
Wed Jun 8 07:24:02 PDT 2011


On Wed, 08 Jun 2011 09:27:21 -0400, Daniel Murphy  
<yebblies at nospamgmail.com> wrote:

> "Trass3r" <un at known.com> wrote in message  
> news:op.vwq80v023ncmek at enigma...
>>
>> ??
>> Shouldn't this just return a delegate?
>
> I don't think getting a delegate makes any sense without an instance.
>
> I'm talking about this case:
> class A
> {
>   void func();
> }
> void main()
> {
>     // returns a nonsense function pointer
>     auto f = &A.func;
>
>     // returns a working delegate
>     auto a = new A();
>     auto g = &a.func;
> }

The reason is to allow composition of delegates (not that I think this is  
a worthy reason):

class A
{
    void func();
    void func2();
}

void main()
{
    auto f = &A.func2;
    auto a = new A();
    auto g = &a.func; // delegate
    g.funcptr = f;
    g(); // calls a.func2()
}

I'd argue that a much more useful return type would be a delegate with the  
this pointer set to null, but then I don't know what funcptr would  
return.  I almost think you need a separate type for it, which seems like  
a lot of effort for something that's hardly used.

-Steve


More information about the Digitalmars-d mailing list