how to do member function pointer in D?

BCS ao at pathlink.com
Wed Jul 30 15:30:34 PDT 2008


Reply to Newbie,

>> 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

same solution as befor (the issue is that the only thing you can put after 
the '=' ouside of a function is a constant expression and a function litteral 
isn't one)

//at global scope (OTOH static's are globals)
static invariant FP mfp1;
static this()
{
mfp1 = cast(invariant FP)(function void(A obj){obj.f();});
}


>> 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.
> 

I still don't get it, what type of manipulation (outside self modifying code) 
can you do that doesn't allow switching in a different function with the 
same signature?





More information about the Digitalmars-d mailing list