title: C++ RTTI and Derived Classes
body: <p>My C++ is a bit rusty.  Here's what I'm attempting to do:</p>
  
  <pre><code>class Cmd { };
  class CmdA : public Cmd { };
  class CmdB : public Cmd { };
  ...
  Cmd *a = new CmdA ();
  Cmd *b = new CmdB ();
  </code></pre>
  
  <p>First problem:</p>
  
  <pre><code>cout &lt;&lt; typeid (a).name ()
  cout &lt;&lt; typeid (b).name ()
  </code></pre>
  
  <p>both return Cmd * types.  My desired result is CmdA* and CmdB*.  Any
  way of accomplishing this other than:</p>
  
  <pre><code>if (dynamic_cast &lt;CmdA *&gt; (a)) ...
  </code></pre>
  
  <p>Second, I would like to do something like this:</p>
  
  <pre><code>class Target {
      public:
          void handleCommand (Cmd *c) { cout &lt;&lt; "generic command..." }
          void handleCommand (CmdA *a) { cout &lt;&lt; "Cmd A"; }
          void handleCommand (CmdB *b) { cout &lt;&lt; "Cmd B"; }
  };
  
  Target t;
  t.handleCommand (a);
  t.handleCommand (b);
  </code></pre>
  
  <p>and get the output "Cmd A" and "Cmd B".  Right now it prints out
  "generic command..." twice.</p>
  
  <p>Thanks </p>