I have a problem with a member array that contains function pointers. Filling the array actually causes no problems, but when comes the time to call the functions that are stored I get a build error.
Here is an extract of my code
StateMachine.h
class StateMachine
{
public:
typedef state_t (StateMachine::*state_func_t)(void);
StateMachine();
virtual ~StateMachine();
void buildStateTable(void);
const state_t& runState(state_t& curState) const;
state_t doStateInit(void);
state_t doStateReady(void);
private:
state_func_t state_table[NUM_STATES];
};
StateMachine.cpp
void StateMachine::buildStateTable(void)
{
state_table[STATE_INIT] = &StateMachine::doStateInit;
state_table[STATE_READY] = &StateMachine::doStateReady;
...
}
const state_t& StateMachine::run_state(state_t& curState) const
{
// 1- here is what I'm trying to do initially but I get a compile error saying that it does not correspond to
// a function call with 0 arguments
state_t newState = *(state_table[curState])();
// 2- I then tried using the '.*' operator as follows (only works if a local StateMachine variable is
// created, which I do not want here), and got a compile error saying I can't convert from 'const
// StateMachine *' to 'StateMachine *const'
state_t newState = (this->*this->state_table[curState])();
return(newState);
}
I simply cannot find a way to call the member functions from the pointers stored in the array without getting some type of error. I don't know exactly what I'm doing wrong.
If I create a local StateMachine object within the function and do the following:
StateMachine mach; state_t newState = (mach.*mach.state_table[curState])();
it builds fine, but I of course do not want to create an instance for every call to this function.
Does anyone have a suggestion to solve this?
Thanks in advance

New Topic/Question
Reply




MultiQuote





|