2

I'm trying to implement a State Machine. The State Machine will have to have an impact on the object that "HAS" it as a member.

However, I obviously can't include the "StateMachine" in the "Game" class AND include the "Game" header in the "StateMachine" class.

How do I get around this problem?

  • possible duplicate of [Resolve circular dependencies in c++](http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c) – dmckee --- ex-moderator kitten Jun 27 '10 at 22:16
  • All of these solutions (all the same :)) are great, but most of what I'm reading seems to suggest that having circular dependencies are a bad thing in any case. So I'm now looking for ways to implement a state machine without the circular dependancy. Again, any help will be welcome, but I can understand if I already used up my question :D – Mike Cordon Jun 27 '10 at 23:08
  • If the problem isn't the inclusion issue, then we're not really looking at a duplicate here...I can't rescind my close vote, but you may consider it disowned. – dmckee --- ex-moderator kitten Jun 27 '10 at 23:27

4 Answers4

0

Use forward class declarations.

DanDan
  • 10,462
  • 8
  • 53
  • 69
0

Forward declare the classes; as an example:

class StateMachine;

class Game
{
    StateMachine *sm;
    // stuff
};
Gemini14
  • 6,246
  • 4
  • 28
  • 32
0

As your client class is a member rather than an inheriting daughter, you can not access any private state of the parent (which I'm sure you knew), so you have a limited number of choices:

  • Expose some state in the parent publicly and diddle that (but this is poor design and should be avoided)
  • Give the parent a public notification interface of some kind and use that (better then the above, but still not great)
  • Pass a call-back to the client (a very c programmer type of solution)
  • Use some kind of signal/slots mechanism to notify the parent that something interesting has happened. If you're using any of the big frameworks (Qt, etc...), this may already be available
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • Is there a better implentation than the one I suggested? The intention was to have states such as "Alive","Dead" or "Zombie" (just as an example) and depending on the state, movement and such would be different. My intention was to abstract each state to a separate class (extending a "state" class) and have the "game" contain the current state. Obviously the problem with this is that the state does not have direct access to the "playerhealth"/"playermovement" type variables and cannot operate "Takehealth" functions either. – Mike Cordon Jun 28 '10 at 00:11
  • @Mike: That seems to scream out for a signals/slots approach. In truth I'm a rank beginner at that kind of programming myself, so I won't try to give detailed advice, but you would have the parent class register to receive signals of various kinds (`AdjustHealth(int)`, etc...) from the member, and the member would send the appropriate signals on changing states. Try [this search](http://stackoverflow.com/search?q=qt+signals+slots) for a lot of stuff on how Qt uses and implements this idea. – dmckee --- ex-moderator kitten Jun 28 '10 at 00:26
  • You've been really helpful so thanks. I'll be looking into everything, I promise :) – Mike Cordon Jun 28 '10 at 00:37
0

To clarify a bit.

If your header file only has pointers of a type defined somewhere else, you don't need to include that header file in your header file.

instead, do a forward declaration such as class StateMachine; then include the StateMachine header file in your CPP file, after the include to your own header file.

Max Kielland
  • 5,627
  • 9
  • 60
  • 95