C++ mutual class dependency

前端 未结 2 1487
臣服心动
臣服心动 2021-01-28 21:48

I am writing a card game in C++. I have a player class, which handles the actions (i.e. selecting a card to lay). If the player is a human, it will use a GUI class<

相关标签:
2条回答
  • 2021-01-28 22:07

    I guess both Player and AI class have common methods for that card game (get card, put card, shuffle, bet, all in...) which can be inherited from a base class CardPlayer or composed during runtime using interfaces (see Strategy Pattern).

    To interface the Human Player class to the GUI, create a suitable interface, for example:

    typedef struct PlayerInfoStruct {
        ---player info variables here---
    };
    
    class IGUIPlayer {
        virtual PlayerInfoStruct GetPlayerInfo(...) = 0;
        ---other methods exclusive to the GUI-player interaction here---
    };
    

    and implement that interface in your HumanPlayer inheriting it:

    class HumanPlayer : public CardPlayer, public IGUIPlayer {
        PlayerInfoStruct GetPlayerInfo() {---method---}
        ---other methods here---
    }
    

    and in your GUI you point to each human player using its interface:

    ---somewhere in your GUI Class---
    IGUIPlayer* humanplayer1 = <pointer to HumanPlayer>;
    

    With that, GUI Class depends on IGUIPlayer. HumanPlayer depends on both IGUIPlayer and CardPlayer. AIPlayer depends only on CardPlayer. No circular stuff!

    Using an interface means that you could, at any moment, provide AI players' info in the GUI, by simply inheriting IGUIPlayer to AIPlayer and implementing the pure virtual methods, without modifying the GUI a single bit. It took me a bit to get used to this (and I still keep on learning), but I can't resist to its inmense power.

    0 讨论(0)
  • 2021-01-28 22:11

    Sometimes it is not necessary to include header for a class in order to use it. For example, if you only use pointer or reference to a class, you can forward-declare it: instead of

    #include "player.h"
    class AI
    {
      std::vector<player *> pl;
    };
    

    you can write

    class player;
    class AI
    {
      std::vector<player *> pl;
    };
    
    0 讨论(0)
提交回复
热议问题