member array of member function pointers..how to call the functions?

call a member function from its pointer stored in a member array

Page 1 of 1

2 Replies - 2192 Views - Last Post: 09 April 2010 - 08:29 AM Rate Topic: -----

#1 LordGee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-April 10

member array of member function pointers..how to call the functions?

Posted 09 April 2010 - 12:34 AM

Hi everyone,

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

Is This A Good Question/Topic? 0
  • +

Replies To: member array of member function pointers..how to call the functions?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: member array of member function pointers..how to call the functions?

Posted 09 April 2010 - 06:01 AM

Perhaps this FAQ entry will be helpful?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5764
  • View blog
  • Posts: 12,580
  • Joined: 16-October 07

Re: member array of member function pointers..how to call the functions?

Posted 09 April 2010 - 08:29 AM

For this kind of thing, I'd use an interface ( well, pure virtual class ).

e.g.
#include <iostream>
#include <queue>

using namespace std;

class StateMachine {
public:
	enum State { uninit, init, running, done };
	struct Process {
		virtual const char *name() const = 0;
		virtual void run(State &result) = 0;
	};
	StateMachine();
	void run();
	const State& runState() const { return currentState; }
private:
	State currentState;
	queue<Process *> pending;
};

struct ProcessDemo : public StateMachine::Process {
	StateMachine::State state;
	void run(StateMachine::State &result) { 
		cout << name() << endl;
		result = state;
	}
	ProcessDemo(StateMachine::State s) : state(s) {}
};

struct InitProcess : public ProcessDemo { 
	const char *name() const { return "InitProcess"; }
	InitProcess() : ProcessDemo(StateMachine::init) {}
};

struct RunProcess : public ProcessDemo { 
	const char *name() const { return "Run1"; }
	RunProcess() : ProcessDemo(StateMachine::running) {}
};


StateMachine::StateMachine() : currentState(StateMachine::uninit) {
	pending.push(new InitProcess());
	pending.push(new RunProcess());
}

void StateMachine::run() {
	cout << "run() called" << endl;
	while (!pending.empty()) {
		Process *proc = pending.front();
		pending.pop();
		proc->run(currentState);
		delete proc;
	}
	currentState = done;
}

int main() {
	StateMachine sm;
	sm.run();
	
	return 0;
}



You could even double, having a single processor do different things depending on that state the instance was created with.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1