Calling an alias for a Class method in another scope

d coder dlang.coder at gmail.com
Fri Jun 1 07:09:49 PDT 2012


Hello Steve


> There is a way, as I hinted :)
>
> I'll show you how, but be prepared to deal with ugliness!
>

All this smart code would be hidden from the end-users so I really do not
care.


> typeof(&F.init.foo) dg; // alternately: void delegate() dg;
> dg.funcptr = &F.foo;    // use the type, not the instance, to get the
> function pointer itself instead of a delegate
> dg.ptr = cast(void *)f; // set f is the context pointer
>
> dg(); // now should call f.foo
>
> Let me understand this. Is F here an alias for foo? If so why I need to
mention stuff like &F.foo? It would defeat my purpose if I have to mention
foo by name. But the code you have put here does give me hope. Just that I
am not able to grasp what F and foo represent here. Probably you mean F is
the type of the class that instantiates the template and foo is the alias
for the method I want to call. Am I right?


> Note that this is *not* a virtual call.  That is currently impossible to
> do without using a lambda.
>

For my purpose that is perfectly fine for now.

Thanks for throwing light on delegates. You have shown me hope :)

- Regards
- Puneet
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120601/829efe61/attachment.html>


More information about the Digitalmars-d mailing list