how to do member function pointer in D?

newbie some at where.com
Wed Jul 30 13:52:36 PDT 2008


> Maybe I'm missing something because what you seem to want doesn't seem useful
> to me, the restriction you seem to want would restrict the allowable values
> for fp to exactly one. If this is so, why do you want a variable?

I want my code to manipulate at function ptr level (instead of just calling the
function).

For now maybe what I can do is declare an invariant variable, which after
initialization cannot be assigned to again.

But I have trouble to get this code compiled:

class A {
  void f() {writefln("f()");}
  void g() {writefln("g()");}
}
alias void function(A) FP;

class B {
  static invariant FP mfp1 = function void(A obj) {obj.f();};  // line 11
}

$ dmd memberfunptr.d
memberfunptr.d(11): Error: cannot implicitly convert expression (__funcliteral1)
of type void function(A) to invariant(void function(A))

Why I need to explicitly cast it?  and even after I change the line to:

  static invariant FP mfp1 = cast(invariant FP)(function void(A obj) {obj.f();});

It says:

$ dmd memberfunptr.d
memberfunptr.d(11): Error: non-constant expression __funcliteral1


Anyone know what's the right way to write this?

Thanks.



More information about the Digitalmars-d mailing list