title: Calling empty virtual function from a base class pointer fails
body: <p>While writing my first piece of software, I encountered a "strange" behavior when playing with virtual functions, inheritance, and pointers. What I had was a template class State, that had a following function in the public interface</p>
  
  <pre><code>void State::saveState() {};
  </code></pre>
  
  <p>When the app was first fired up, in order to set a default state, it was calling a <code>setState()</code> function within the manager function, that roughly goes as follows</p>
  
  <pre><code>case(some enum):
  {
      mActiveState-&gt;saveState();
      delete mActiveState; 
      mActiveState = new SomeStateClassDerivedFromState();
      mActiveState-&gt;setup(some arguments);
      break;
  }
  </code></pre>
  
  <p>Now for the strange thing - the first two lines of this code should cause a run-time error, or something similar. If this function is used to CHANGE the state, then it should be fine. But it was also used for the assignment of some object/memory for <code>State::mActiveState</code>, which at that point is <code>nullptr</code>. But it didn't crash for some reason, program executed as expected. But then, I wanted to override the <code>saveState()</code> function in one of the classes inheriting from State, so I added a virtual tag to the function (body remained empty), and wrote a definition for <code>saveState()</code> in the derived class. Suddenly, these two lines started crashing the app at startup (as expected). Removing the virtual tag from <code>saveState()</code> made the app run again.</p>
  
  <p>Why didn't calling a member function and deleting a <code>nullptr</code> cause a crash until the <code>saveState()</code> member function was declared as virtual (with empty body, not pure virtual)?</p>
  
  <p>This code recreates the problem</p>
  
  <pre><code>class State
  {
  public:
  void saveState() {};
  }
  
  class DState : public State
  {
  saveState() {};
  }
  
  int main()
  {
  State* state = nullptr;
  state-&gt;saveState();
  delete state;
  state = new DState();
  }
  </code></pre>
  
  <p>This worked. The moment I added virtual tag to the <code>saveState()</code> function in the State class, it crashed on start. My question again, why?</p>