-1

I don't what to manage or less as I can manage raw pointers note in the example I have to delete the object before removing 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);
}
6
  • 4
    m_gameStates.pop_back(pState); was never valid code, did you mean push_back? Commented Feb 3, 2023 at 9:43
  • class MenuState : GameState also looks wrong. Do you mean class MenuState : public GameState ? Commented Feb 3, 2023 at 9:54
  • MenuState should publicly inherit GameState in order to assign new MenuState() to a GameState* gs. Commented Feb 3, 2023 at 9:55
  • 2
    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? Commented Feb 3, 2023 at 9:56
  • 1
    don't forget the virtual destructor for GameState (required) Commented Feb 3, 2023 at 10:29

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.