title: Visitor: adding more types via inheritance
body: <p>I want to expand a declared Visitor through inheritance and have the run-time environment search the descendents of the Visitor for the correct method to execute.  I can have this in C#, but I'm looking to use it in C++.  I tried the following code in g++ and the descendent method is not called; only the method for the base class is called.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  struct Field; // Forward declaration
  
  struct Visitor
  {
      virtual void visit(Field&amp; f) = 0; // Visits Field objecs and objects derived from Field.
  };
  
  struct Field_String;
  
  struct Visitor_Field_String : public Visitor
  {
      // Extend the Visitor by specifying a visitation
      //     for Field_String
      virtual void visit(Field_String&amp; fs) = 0;
  };
  
  struct Field
  {
      void accept_visitor(Visitor&amp; v)
      {
          cout &lt;&lt; "Field accepting visitor.\n";
          v.visit(*this);
      }
  };
  
  struct Field_String : public Field
  {
      void accept_visitor(Visitor&amp; v)
      {
          cout &lt;&lt; "Field_String accepting visitor.\n";
          v.visit(*this);  // Line 1
      }
  };
  
  struct Full_Visitor : Visitor_Field_String
  {
      void visit(Field&amp; f)
      {
          cout &lt;&lt; "Visiting a Field object\n";
          return;
      }
      void visit(Field_String&amp; fs)
      {
          cout &lt;&lt; " Visiting a Field_String object\n";
          return;
      }
  };
  
  
  int main(void)
  {
      Field_String fs;
      Full_Visitor visitor;
      fs.accept_visitor(visitor);
      return 0;
  }
  </code></pre>
  
  <p>I get the following output:</p>
  
  <pre><code># g++ --version
  g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
  Copyright (C) 2004 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  # g++ -o virtual_visitor.exe virtual_visitor.cpp
  # ./virtual_visitor.exe
  Field_String accepting visitor.
  Visiting a Field object
  </code></pre>
  
  <p>The output I want is:</p>
  
  <pre><code>Field_String accepting visitor.
  Visiting a Field_String object
  </code></pre>
  
  <p>My two questions:</p>
  
  <ol>
  <li>Why is the <code>visit</code> method in the
  descendent Visitor not executed?</li>
  <li>How do I execute the <code>visit</code> method
  in the descendent Visitor using
  polymorphism?</li>
  </ol>
  
  <p><em>Note:  The objective is to reduce the classes specified in a Visitor class, by using inheritance and allowing for the cases that may not use all of the classes specified in the Visitor.</em>  </p>
  
  <p><em>Note:  This is not double dispatch, but extended dispatch.</em></p>