Bind object to member function
Kirk McDonald
kirklin.mcdonald at gmail.com
Sun Aug 13 15:28:01 PDT 2006
Li Jie wrote:
> class Test{
> void a(int n){}
> }
>
> auto a = &Test.a;
> auto t = new Test;
> a(t, 1); // or: a.bind(t); a(1);
>
> Is this supported?
You want pointers to member functions, as C++ has. D does not have
these. Rather, it has delegates, which are often more useful:
class Test {
void a (int n) {}
}
Test t = new Test;
void delegate(int) dg = &t.a;
dg(1);
This use of delegates is documented here:
http://www.digitalmars.com/d/function.html#closures
If you're looking for pointers to member functions to implement some
sort of dispatch mechanism, delegates do not directly allow this.
However, there do exist hacks to allow it.
--
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki
More information about the Digitalmars-d-learn
mailing list