title: C++ Command line action abstraction using interface
body: <p>I'm building an application whose usage is going to look something like this:</p>
  
  <pre><code>application --command --option1=? --option2=2?
  </code></pre>
  
  <p>Basically, there can be any number of options, but only one command per instance of the application. Similar to the way git works.</p>
  
  <p>Now, I thought I'd write it in C++ to get some boost and stl experience and have a go with a few of those design patterns I keep reading about. So, I implemented this:</p>
  
  <pre><code>class Action
  {
  public:
      void AddParameter(std::string key, boost::any p);
      virtual unsigned int ExecuteAction();
  
  protected:
      std::map&lt;std::string, boost::any&gt; parameters;
  };
  </code></pre>
  
  <p>I'll explain my logic anyway, just to check it - this is an abstract-ish action. All actions need option adding, hence the parameters map, so that we can implement at this level, but we expect <code>ExecuteAction</code> to be implemented by derived classes, such as my simple example <code>DisplayHelpAction</code>, which does pretty much what it says on the tin.</p>
  
  <p>So now I've written a factory, like so:</p>
  
  <pre><code>class DetermineAction
  {
  public:
      DetermineAction();
      vx::modero::Action getAction(std::string ActionString);
  private:
      std::map&lt;std::string, vx::modero::Action&gt; cmdmap;
  };
  </code></pre>
  
  <p>The logic being that the constructor will create a map of possible strings you can ask for and getAction will do what it says - give it a command string and it'll give you a class that is derived from <code>Action</code> which implements the desired functionality.</p>
  
  <p>I'm having trouble with that constructor. I am trying this:</p>
  
  <pre><code>this-&gt;cmdmap = std::map&lt;std::string, Action&gt;();
  this-&gt;cmdmap.insert(pair&lt;string, Action&gt;("help", DisplayHelpAction()));
  this-&gt;cmdmap.insert(pair&lt;string, Action&gt;("license", DisplayLicenseAction()));
  </code></pre>
  
  <p>Which is causing a lot of errors. Now, I'm used to the Java Way of interfaces, so you use:</p>
  
  <pre><code>Interface I = new ConcreteClass();
  </code></pre>
  
  <p>and Java likes it. So that's the sort of idea I'm trying to achieve here, because what I want do have for the implementation of <code>getAction</code> is this:</p>
  
  <pre><code>return this-&gt;cmdmap[ActionString];
  </code></pre>
  
  <p>Which should return a class derived from Action, on which I can then start adding parameters and call execute.</p>
  
  <p>So, to summarise, I have two questions which are closely related:</p>
  
  <ul>
  <li>Soundboard. I'm deliberately practising abstracting things, so there's some additional complexity there, but in principle, is my approach sound? Is there an insanely obvious shortcut I've missed? Is there a better method I should be using? </li>
  <li><p>How can I set up my class mapping solution so that I can return the correct class? The specific complaint is link-time and is:</p>
  
  <pre><code>Linking CXX executable myapp
  CMakeFiles/myapp.dir/abstractcmd.cpp.o: In function `nf::Action::Action()':
  abstractcmd.cpp:(.text._ZN2vx6modero6ActionC2Ev[_ZN2vx6modero6ActionC5Ev]+0x13): undefined reference to `vtable for nf::Action'
  </code></pre></li>
  </ul>
  
  <p>Just because it might be relevant, I'm using <code>boost::program_options</code> for command line parsing. </p>
  
  <hr>
  
  <p><strong>Edit 1</strong>: Ok, I have now replaced <code>Action</code> with <code>Action*</code> as per Eugen's answer and am trying to add <code>new SomethingThatSubclassesAction</code> to the map. I'm still getting the vtable error.</p>