title: C++ Dynamic Dispatch without Virtual Functions
body: <p>I've got some legacy code that, instead of virtual functions, uses a <code>kind</code> field to do dynamic dispatch.  It looks something like this:</p>
  
  <pre><code>// Base struct shared by all subtypes
  // Plain-old data; can't use virtual functions
  struct POD
  {
      int kind;
  
      int GetFoo();
      int GetBar();
      int GetBaz();
      int GetXyzzy();
  };
  
  enum Kind { Kind_Derived1, Kind_Derived2, Kind_Derived3 /* , ... */ };
  
  struct Derived1: POD
  {
      Derived1(): kind(Kind_Derived1) {}
  
      int GetFoo();
      int GetBar();
      int GetBaz();
      int GetXyzzy();
  
      // ... plus other type-specific data and function members ...
  };
  
  struct Derived2: POD
  {
      Derived2(): kind(Kind_Derived2) {}
  
      int GetFoo();
      int GetBar();
      int GetBaz();
      int GetXyzzy();
  
      // ... plus other type-specific data and function members ...
  };
  
  struct Derived3: POD
  {
      Derived3(): kind(Kind_Derived3) {}
  
      int GetFoo();
      int GetBar();
      int GetBaz();
      int GetXyzzy();
  
      // ... plus other type-specific data and function members ...
  };
  
  // ... and so on for other derived classes ...
  </code></pre>
  
  <p>and then the <code>POD</code> class's function members are implemented like this:</p>
  
  <pre><code>int POD::GetFoo()
  {
      // Call kind-specific function
      switch (kind)
      {
      case Kind_Derived1:
          {
          Derived1 *pDerived1 = static_cast&lt;Derived1*&gt;(this);
          return pDerived1-&gt;GetFoo();
          }
      case Kind_Derived2:
          {
          Derived2 *pDerived2 = static_cast&lt;Derived2*&gt;(this);
          return pDerived2-&gt;GetFoo();
          }
      case Kind_Derived3:
          {
          Derived3 *pDerived3 = static_cast&lt;Derived3*&gt;(this);
          return pDerived3-&gt;GetFoo();
          }
  
      // ... and so on for other derived classes ...
  
      default:
          throw UnknownKindException(kind, "GetFoo");
      }
  }
  </code></pre>
  
  <p><code>POD::GetBar()</code>, <code>POD::GetBaz()</code>, <code>POD::GetXyzzy()</code>, and other members are implemented similarly.</p>
  
  <p>This example is simplified.  The actual code has about a dozen different subtypes of <code>POD</code>, and a couple dozen methods.  New subtypes of <code>POD</code> and new methods are added pretty frequently, and so every time we do that, we have to update all these <code>switch</code> statements.</p>
  
  <p>The typical way to handle this would be to declare the function members <code>virtual</code> in the <code>POD</code> class, but we can't do that because the objects reside in shared memory.  There is a lot of code that depends on these structs being plain-old-data, so even if I could figure out some way to have virtual functions in shared-memory objects, I wouldn't want to do that.</p>
  
  <p>So, I'm looking for suggestions as to the best way to clean this up so that all the knowledge of how to call the subtype methods is centralized in one place, rather than scattered among a couple dozen <code>switch</code> statements in a couple dozen functions.</p>
  
  <p>What occurs to me is that I can create some sort of adapter class that wraps a <code>POD</code> and uses templates to minimize the redundancy.  But before I start down that path, I'd like to know how others have dealt with this.</p>