using function pointers and polymorphism

I'm trying to create a state machine; I have a base class State, and another class T that inherits publicly from State. State includes a function pointer. within class T, I want the function pointer to point to a member function of T.

In other words, i Have these headers:
1
2
3
4
5
6
7
8
9
10
11
12
class State {
	State (*stateMethod)();
};

class T : public State { 
	public:
		T() { }
		T(char*);
		~T() { }
	private:
		State display();
};

in T.cpp I have:
1
2
3
T::T(char* file) {
	stateMethod = &T::display;
};


I get the error: "cannot convert `State (T::*)()' to `State (*)()' in assignment "

is there a simple way around this? am I missing something obvious?
Last edited on
It seems you cant just point a State (*fucPtr)() too TitleScreen::display. You would need a State (TitleScreen::*fucPtr)(). So unfortunately, I don't see a workaround.
http://www.newty.de/fpt/functor.html

Functors ftw

Took me a bit to figure out the point, but once I got it it's really cool. Takes a bit of work to set up though.
*sigh* yeah, I saw that...I was really really hoping there was a simpler way than wrapping display into a static function and overloading operator().

Thanks.

--OW
er... I might be misunderstanding what you said... but you wouldn't have to make it static. That would kind of defeat the point.

Anyway... looking again at your original problem... it looks like this could easily be solved with some polymorphism:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class State
{
public:
  virtual State display() = 0;
};

class T : public State
{
public:
  virtual State display()
  {
    //do whatever, here
  }
};


Or is this not what you're trying to do? Also State seems like a strange return type for that.
yes, i was confusing the static part with another solution I read to a different problem. ^_^'

That might work, but it would be kind of pointless, as not every type (child class) of State will have a display() function.

The idea is that upon executing the method that is associated with a state, it will return the next state of the state machine.
Last edited on
but every one would have a 'stateMethod' function, right? Couldn't you just put that in every derived class and have it call whatever it needs to from there (in the above example, T::stateMethod could call T::display, but other child classes would do something else with stateMethod).
Well, stateMethod is a pointer to a function, not an actual function, and I wanted this thing to be as flexible as possible, but at this point, I'll probably end up doing that and deal with the issue of changing the target function when and if it ever comes up.

Thanks.
I'm just wondering from a practical standpoint if what you need really is a function pointer. If every child class is going to have a function like this, then that's pretty much exactly what pure virtual functions are for.

edit:

it's also worth noting that virtual functions basically compile to function pointers... so it might actually be what you want, just with simpler syntax than the alternatives.
Last edited on
Right, see this program was originally much simpler, and written in Python, so I was trying to stay true to the function pointer approach, because that's how it was originally written, and it was working; A bit of a redesign seems prudent though.
Might I suggest using boost::function and boost::bind to solve this problem simply.

1
2
3
4
5
6
7
8
9
10
11
12
13
class State {
    typedef boost::function<State(void)> Func;
    Func stateMethod;

  public:
    explicit State( const Func& fn = Func() ) : stateMethod( fn ) {}
};


// ...
T::T() : State( boost::bind( &T::display, this ) )
{
}

Topic archived. No new replies allowed.