title: C++: how to write this abstract class?
body: <p>I want to define an abstract class for a finite state machine.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>class State
  {
    virtual State transit(const Input &amp;i) = 0;
  };
  </code></pre>
  
  <p>The idea is that I want a prototype function "transit" which takes the input and transit to the next state. Unfortunately, compiler won't allow this.</p>
  
  <p>Is there a way to provide an abstract class like that?</p>
  
  <p>What I think is to provide an abstract of state machine, the logic of how to run the state machine doesn't need to change for a set of problems, like taking the input and moving on to the next state, etc. But this abstraction doesn't care how state and input are defined, maybe something like below.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>template&lt;S, I&gt;
  class StateMachine
  {
    public:
      void set_initial_state(const S &amp;is)
      {
          current_state = is;
      }
      void take_input(const I &amp;input)
      {
          S next_state = current_state.transit(input);
          current_state = next_state;
      }
    private:
      S current_state;
  };
  </code></pre>
  
  <p>When using this template, I would like to provide more hint to "S". For example, it must have a "transit" function. That's what I come for the idea of abstract class of "State".</p>