Pointers to member functions part 2

Daniel Murphy yebblies at nospamgmail.com
Sat Jun 18 23:44:01 PDT 2011


D does not have member function pointers, but currently the following will 
have a and b be of type 'void function()'.

class C { void func(); }

void main()
{
    auto c = new C;

    auto a = &C.func;
    auto b = (&c.func).funcptr;
}

I'd like to change &ClassType.nonstaticmemberfunction and delegate.funcptr 
to both return void* and make delegate.ptr and delegate.funcptr both 
read-only.  This would disallow the following nonsense code.

class Class
{
    void func() {}
}
class OtherClass
{
    void func() {}
}

void main()
{
    auto instance = new Class();
    void delegate x = &instance.func;
    x.ptr = new int;
    x.funcptr = &OtherClass.func;
    x();
    auto funptr  = &Class.func;
    funptr();
}

You can still of course modify delegates using casts or unions if you have 
to, but as it's something unsafe you shouldn't be able to do it without 
them.

Can anybody see a reason these changes wouldn't be an improvement to the 
language?
If not I'll put in a pull request for it. 




More information about the Digitalmars-d mailing list