how to do member function pointer in D?
BCS
ao at pathlink.com
Wed Jul 30 14:18:21 PDT 2008
Reply to Newbie,
>> 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
> }
[...]
> 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
to clear that error:
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>
More information about the Digitalmars-d
mailing list