Curnelious Curnelious - 4 months ago 17
C++ Question

Calling function pointer from static function

Inside some class called

Light
, I have a static function.

I would like to "fire" a delegate from it ,

Inside Light.h

static float intepreterDelegate(char *arg){



// here I need to call the function pointer inside Light itself
Light b;
return b.fpAction(arg); // ** error: "expected unqualified id"
};

float (*fpAction)(char*) = 0 ; // the actual pointer


How would I create the right syntax for this ?

b.(*fpAction)("arg");


EDIT:

(b.*b.fpAction)(arg);


ERROR: right hand operator to * has non.

Answer
float (*fpAction)(char*) = 0 ; // the actual pointer 

this creates a regular function pointer, not a member-function pointer. change to

float (Light::*fpAction)(char*) = 0 ;

calling this function pointer on the Light instance named b

float result = (b.*b.fpAction)("arg");

P.S. if you're wondering what the double b is doing there. it's really (b.*(b.fpAction))("arg"); b.fpAction identifies the pointer as member of Light instance b. (b.*pointer)("arg") calls the functionpointer using 'b' as 'this' value inside the function.