Pointers to non-static member functions!

Daniel Gibson metalcaedes at gmail.com
Wed Jun 8 08:54:01 PDT 2011


Am 08.06.2011 16:54, schrieb Steven Schveighoffer:
> 
> I almost would prefer that hacking delegates would be illegal.  Yes, you
> can get the function pointer and data pointer out of a delegate, but
> cannot set them.  I can't think of any good use cases for them.
> 
> I'm mixed on whether you should be able to get a member "function
> pointer".  It would be nice to simply make it a normal function pointer
> that you can call with the appropriate type.

That'd be nice. I think having a possibility to get a pointer to a
member function that you can use with any instance of the class the
method belongs to would be nice. Currently this should be possible by
using a delegate and changing its .ptr to the object you want to call
the method on (this won't work anymore if the delegates internal
pointers are read-only as suggested above).
This could be used for something like runtime-reflection (like Java's
Method class - which is horribly slow btw, compared to a direct call to
a method. For whatever reason.)
So you could use object.classinfo.getMembers(), look for the method you
want to use (with the name given at runtime!) and then use a function
pointer to that Method to efficiently call it on any object of the same
type. (So much for the theory, I don't see a way to find out the methods
signature to match it to the signature you want, especially for
overloaded methods - but maybe I just missed it.)

BTW: MemberInfo (and MemberInfo_field and MemberInfo_function) should be
documented in the object documentation.
And what does MemberInfo_function's typeInfo() return?
And is there a way to get a methods signature from MemberInfo_function?

(I hope this made sense and I didn't ignore a nicer way to do this - at
runtime - that is already implemented)

Cheers,
- Daniel


More information about the Digitalmars-d mailing list