Linked Questions

Popular Questions

I'm creating a state machine that allows you to add states onto a stack. Here are a couple methods it contains:

void pushState(State* state);

State* getCurrentState();

State is just a base class that is meant to be used to create derived classes. So I created a derived class called GameState and it has a unique method called foo. If I do something like the following, it won't let me access foo because "No method named 'foo' exists for class State.

State* currentState = myStateMachine.getCurrentState();

I also tried changing State* to GameState*, but it doesn't work because getCurrentState() returns type State. How can I get the current state on the stack when I don't actually know what derived state is the current one?

I also don't know why it allows me to push GameState onto a stack that is expecting State, but won't let me retrieve it as GameState. Instead it makes me retrieve it at State.

Related Questions