Pointers to non-static member functions!

Michel Fortin michel.fortin at michelf.com
Wed Jun 8 03:29:24 PDT 2011


On 2011-06-08 04:00:53 -0400, "Daniel Murphy" <yebblies at nospamgmail.com> said:

> Currently, this compiles:
> 
> class A
> {
>     void func() {}
> }
> 
> void main()
> {
>   auto p = &A.func;
> }
> 
> In this case, typeof(p) is void function().
> Calling one of these pointers, of course, can easily give you a segfault as
> it completely ignores the this pointer.
> I didn't even realize you could do this until I saw
> http://d.puremagic.com/issues/show_bug.cgi?id=5986
> 
> I understand that it could be useful to have a way to get the address of a
> non-static member function, but I doubt returning a malformed function
> pointer is really useful.
> 
> Does anybody think changing &ClassType.nonStaticMember to return void* would
> be a bad idea?

I think it should work like my D/Objective-C language extension does it 
for selectors.
<http://michelf.com/projects/d-objc/syntax/#selector-literals>

&A.func should return a *member* function pointer which wants a 'this' 
parameter, and when you call it you should be required to add the 
'this' argument at the beginning of the argument list, like this:

	void main() {
		auto p = &A.func;
		p(new A);
	}

That'll probably require a syntax to specify a function type that wants 
a this pointer.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list