advanced function binding
Moritz Warning
moritzwarning at web.de
Thu Mar 29 10:54:17 PDT 2007
dg.funcptr = cast(FuncPtr) &Func;
The compiler resists the cast and gives an error again.
So I get this error:
Main.d:104: Error: this for getBar needs to be type Foo not type Main.Wrapper!(Foo,getBar).Wrapper
Main.d:65: template instance Main.Wrapper!(Foo,getBar) error instantiating
Kirk McDonald Wrote:
>
> Oh! I've seen this before. When you take the address of a member
> function, any member function, even one of an unrelated class, from
> inside a class, the compiler wants to take it as a delegate with 'this'
> as the context. It then complains that 'this' is of the wrong type, as
> you see in the first error. For a better feeling, try cast(FuncPtr) instead.
>
> One other thing: You can re-write that assignment using D's type
> inference. You can also drop the trailing parentheses.
>
> auto x = new Wrapper!(Foo, Foo.getBar);
>
> Isn't that much better? :-)
Yes, it is! =)
>
> --
> Kirk McDonald
> http://kirkmcdonald.blogspot.com
> Pyd: Connecting D and Python
> http://pyd.dsource.org
More information about the Digitalmars-d-learn
mailing list