0

i dont what to manage or less as i can manage raw pointers note in the example i have to delete the object before removeing it from the vector i want to avoid it it here and later what will be good case to convert this code to using unique_ptr or shared_ptr

class GameState
{
    public:
        virtual bool onEnter() = 0;
        virtual bool onExit() = 0;
        virtual std::string getStateID() const = 0;
};

class MenuState : GameState
{
        public:
             MenuState(){};
             virtual ~MenuState(){};             
             bool onEnter(){};
             bool onExit(){};
             std::string getStateID() const;
        private:
             static const std::string s_menuId;

};

class StateMechine
{
    
    public:
        void pushState(GameState* pState)
        {
            m_gameStates.pop_back(pState);
            m_gameStates.back()->onEnter();
        }

        void changeState(GameState* pState)
        {
            if(!m_gameStates.empty())
            {
                if(m_gameStates.back()->onExit())
                {
                    delete m_gameStates.back();
                    m_gameStates.pop_back();
                }
            }
        }

         
    private:
        std::vector<GameState*> m_gameStates;
}

int main(int argc ,char** argv)
{
    GameState *gs  = new MenuState();
    StateMechine sm;
    sm.pushState(gs);
    sm.changeState(gs);
}
user63898
  • 29,839
  • 85
  • 272
  • 514
  • 4
    `m_gameStates.pop_back(pState);` was never valid code, did you mean `push_back`? – Caleth Feb 03 '23 at 09:43
  • `class MenuState : GameState` also looks wrong. Do you mean `class MenuState : public GameState` ? – 463035818_is_not_an_ai Feb 03 '23 at 09:54
  • `MenuState` should publicly inherit `GameState` in order to assign `new MenuState()` to a `GameState* gs`. – wohlstad Feb 03 '23 at 09:55
  • 1
    please post the actual code. Errors in code that are unrelated to the question are not nice. It looks like you never actually tried to compile this code, so why bother to translate it to newer standard? – 463035818_is_not_an_ai Feb 03 '23 at 09:56
  • `bool onEnter(){};` is also something that my compiler refuses to let pass. if you want to present a dummy make it `bool onEnter(){ return true; };`, because the empty implementation `{}` spoils the whole code with undefined behavior – 463035818_is_not_an_ai Feb 03 '23 at 09:58
  • 1
    don't forget the virtual destructor for GameState (required) – jls28 Feb 03 '23 at 10:29

1 Answers1

1

The std::vector<GameState*> can be replaced with a std::vector<std::unique_ptr<GameState>>. That way a call to m_gameStates.pop_back() will delete the corresponding object.

class StateMechine
{
    
    public:
        void pushState(std::unique_ptr<GameState> pState)
        {
            m_gameStates.push_back(std::move(pState));
            m_gameStates.back()->onEnter();
        }

        void changeState()
        {
            if(!m_gameStates.empty())
            {
                if(m_gameStates.back()->onExit())
                {
                    m_gameStates.pop_back();
                }
            }
        }

         
    private:
        std::vector<std::unique_ptr<GameState>> m_gameStates;
};

int main(int argc ,char** argv)
{
    StateMechine sm;
    sm.pushState(std::make_unique<MenuState>());
    sm.changeState();
}
Johann Schwarze
  • 161
  • 1
  • 4