Stack-based state machine Is everything alright with this code? What can I add to it?

state.h:

#pragma once

class State
{
public:
    virtual void update(float dt) = 0;

    virtual void draw(float dt) = 0;
};


state_machine.h:

#pragma once
#include &lt;memory&gt;
#include &lt;stack&gt;
#include "state.h"

using StateRef = std::shared_ptr&lt;State&gt;;

class StateMachine
{
public:
    void push_state(const StateRef&amp; state);

    void push_state(State* state);

    template&lt;class T&gt;
    void push_state();

    StateRef pop_state();

    StateRef get_current_state() const;

    void update(float dt);
private:
    std::stack&lt;StateRef&gt; stack;
};

template&lt;class T&gt;
inline void StateMachine::push_state()
{
    push_state(StateRef(new T));
}


state_machine.cpp:

#include "state_machine.h"
#include "state.h"

void StateMachine::push_state(const StateRef&amp; state)
{
    stack.emplace(state);
}

void StateMachine::push_state(State* state)
{
    push_state(StateRef(state));
}

StateRef StateMachine::pop_state()
{
    auto state = get_current_state();

    stack.pop();

    return state;
}

StateRef StateMachine::get_current_state() const
{
    return stack.top();
}

void StateMachine::update(float dt)
{
    if(!stack.empty())
    {
        auto state = get_current_state();

        state-&gt;update(dt);
        state-&gt;draw(dt);
    }
}


Dummy usage example:

#include &lt;sm/state_machine.h&gt;
#include &lt;iostream&gt;

struct Foo: public State
{
    void update(float dt) override
    {
        std::cout &lt;&lt; "(updated)" &lt;&lt; std::endl;
    }

    void draw(float dt) override
    {
        std::cout &lt;&lt; "(drawed)" &lt;&lt; std::endl;
    }
};

int main()
{
    StateMachine machine;

    machine.push_state&lt;Foo&gt;();

    machine.update(0.0f);

    machine.pop_state();

    machine.update(0.0f);

    return 0;
}


GitHub repo

How can I add and use "Controller" component? (component that handles input and etc.). Is it necessary? (MVC pattern)
Good stuff, I particularly like State being a purely abstract interface, that's exactly how to best do modern inheritance.

State needs a virtual destructor

This is 100% required in this case, since you delete states from pointers of State.

Reduce API surface

You have too many push_state() functions

void push_state(const StateRef&amp; state);
void push_state(State* state);

template&lt;class T&gt;
void push_state();


You would be better off having just the single:

void push_state(const StateRef&amp; state);


What looks like "convenience" to you just ends up being "confusing" for possible users of the class as they have to wonder "which one of these should I be using?"

Take objects by value if you are taking ownership

void push_state(const StateRef&amp; state);


should be:

void push_state(StateRef state);


Your function currently will always go through a copy-constructor, and for shared_ptr&lt;&gt; this is definitely non-trivial as that has to be thread safe. 

By taking the StateRef by value, this gives users control over whether to provide a RValue, move in a LValue, or provide a copy of some other object. Maximum flexibility without any overloads.

This brings us to:

Prefer unique_ptr over shared_ptr

I see no shared ownership scemantics here, unique_ptr would be better simply for performance reasons. But you may have other parts of the larger codebase that warrant it. That comment is just about the code I see in isolation.

That's pretty much all I've got here.