title: Understanding double dispatch C++
body: <p>I try to understand how double dispatch works. I created an example where a monster and a warrior derived from the abstract class Creature could fight. The class Creature has method "fight", which is defined in derived classes, and in each derived class is defined what happens if warrior fights with warrior or with monster etc. I wrote the following code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  class Monster;
  class Warrior;
  
  class Creature{
  public:
      virtual void fight(Creature&amp;) =0;
  };
  
  class Monster: public Creature{
      void fightwho(Warrior&amp; w) {cout&lt;&lt;"Monster versus Warrior"&lt;&lt;endl; }
      void fightwho(Monster&amp; m) {cout&lt;&lt;"Monster versus Monster"&lt;&lt;endl; }
  public:
      void fight(Creature&amp; c)  {c.fightwho(*this);}
  };
  
  class Warrior: public Creature{
      void fightwho(Warrior&amp; w) {cout&lt;&lt;"Warrior versus Warrior"&lt;&lt;endl; }
      void fightwho(Monster&amp; m) {cout&lt;&lt;"Monster versus Warrior"&lt;&lt;endl; }
  public:
      void fight(Creature&amp; c) {c.fightwho(*this);}
  };
  
  int main()
  {
  Warrior w;
  Monster m;
  w.fight(m);
  }
  </code></pre>
  
  <p>This results in compiler error, which I foresee:</p>
  
  <p>ex12_10.cpp: In member function ‘virtual void Monster::fight(Creature&amp;)’: ex12_10.cpp:17:30: error: ‘class Creature’ has no member named ‘fightwho’ </p>
  
  <p>ex12_10.cpp: In member function ‘virtual void Warrior::fight(Creature&amp;)’: ex12_10.cpp:24:29: error: ‘class Creature’ has no member named ‘fightwho’</p>
  
  <p>But I don't know how to proceed from here... Please help.</p>