C++ mutual class dependency

前端 未结 2 1489
臣服心动
臣服心动 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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 pl;
    };
    

    you can write

    class player;
    class AI
    {
      std::vector pl;
    };
    

提交回复
热议问题