how to do member function pointer in D?

newbie some at where.com
Wed Jul 30 15:11:45 PDT 2008


> to clear that error:
> static invariant FP mfp1;
> static this()
> {
>  mfp1 = cast(invariant FP)(function void(A obj){obj.f();});
> }

Thanks.

Two more questions:

1) why the cast is necessary? shall I report a bug? e.g.

invariant int a = 1;  // I don't need the cast here!

2) how to do it as global variables? e.g.

If I pull out it from B, and define as global variable:

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

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


> OTOH I still have no clue what you are trying to do. I suspect their is a
> simperer way to do what you want.
> That said, /I/ often /enjoy/ doing things the far from simple way. <g>

I need to hold a collection of member-function pointers, and manipulate it in some
way, then later make function calls on different object via these pointers.

I want to protect myself that any these pointers shouldn't be re-assigned to other
member functions with the same signatures.




More information about the Digitalmars-d mailing list