I don't know why your code(function pointer) is taking StateMachine as argument. Perhaps an instance of StateMachine is going as an argument to method of another StateMachine instance, right?
Also, you are accessing a class variable (the array "
state_machine") from outside of class variable which is not good. You should declare the array as private/protected for that class and use member function to access it.
Anyway, following is the example code which shows how to declare, assign and access member-function pointer.
#define MAX_EVENTS 5
#define MAX_STATES 5
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
class StateMachine; // froward declaration
typedef int (StateMachine::*state_machine) (StateMachine *arg1, int arg2, int arg3);
class StateMachine
{
public:
StateMachine()
{
// initialize function pointers array here
stateMachines[0][0] = &StateMachine::method1;
stateMachines[0][1] = &StateMachine::method2;
// remainings will go here...
}
state_machine stateMachines[MAX_EVENTS][MAX_STATES];
int callFunctionPointer(int row, int col, StateMachine* arg1, int arg2, int arg3)
{
return (this->*(stateMachines[0][0]))(arg1, arg2, arg3);
}
private:
int method1(StateMachine* sm, int arg2, int arg3)
{
// use sm here
return 0;
}
int method2(StateMachine* sm, int arg2, int arg3)
{
// use sm here
return 0;
}
};
int main()
{
StateMachine sm1;
StateMachine sm2;
int returnValue = CALL_MEMBER_FN(sm1, sm1.stateMachines[0][0])(&sm2, 1, 2); // this is good as it is readable to the readers of the code
returnValue = (sm1.*(sm2.stateMachines[0][0]))(&sm2, 1, 2); // this is bad
returnValue = sm1.callFunctionPointer(0,0, &sm2, 1, 2); // this is good too; anyone who has allergy with macro, can use this way.
return 0;
}
Hope this help you!