Is there any kind of “expression class” (C++)

前端 未结 9 1407
眼角桃花
眼角桃花 2020-12-13 14:45

I am creating a game that lets the player enter input, changes some states, then checks if a \"goal value\" is true (obviously this description is muchly simplified), and I

9条回答
  •  自闭症患者
    2020-12-13 15:21

    Why not build your own expression classes?

    class GoalBase
    {
        virtual bool goal() = 0;
    };
    
    class Enemies : public GoalBase 
    {
       // ..
       private:
          int enemies_;
    
       public:
          Enemies(int start) : enemies_(start) {}
          void kill() { if (enemies_) --enemies_; }
          bool goal() { return enemies_ == 0; }
    };
    
    int main()
    {
        Enemies enemiesToKill(5);
        enemiesToKill.kill();    
    
        // ..
        if (enemiesToKill.goal()) {
            // ..
        }
    
        return 0;
    }
    

    Other classes could have other methods, parameters, operators etc. Use your imagination.

提交回复
热议问题