how to do member function pointer in D?

Russell Lewis webmaster at villagersonline.com
Wed Jul 30 10:27:42 PDT 2008


Although people have presented hacks to do this, there isn't a standard 
way to do this in D.

Of course, you could use a function literal to accomplish this:
   mfp = function void(A obj) { obj.f(); };
That isn't techically a member function pointer, but it is pretty similar.

Do you have a compelling use case where you need member-function-pointer 
but delegates or the function literal above wouldn't work?  I haven't 
yet heard of one, but of course they might exist. :)

Russ

newbie wrote:
> Hi,
> 
> I want to be able to create a member function pointer type from a class (not
> from an object of that class), i.e.
> 
> class A {
>   void f() {}
>   void g() {}
> }
> 
> (member function pointer) mfp = &A.f; // what's the sytax for the type?
> 
> // also I want the compiler to report error if I pass g() to mfp:
> mfp = &A.g;   // type error!
> 
> // and mfp can be invoked on different object instance of A:
> A a1, a2;
> 
> mfp(a1);   // call a1.f(), what's the right syntax?
> mfp(a2);   // call a2.f()
> 
> Thanks.



More information about the Digitalmars-d mailing list