title: Array of functions that can be called like 'funs[1]();'
body: <p>I'm working on a Visual C++ 2010 Express console application.</p>
  
  <p>Before I go into detail, the summary here is: How can I make an array/list/vector of functions and call them from that array?</p>
  
  <p>So I'm having a little difficulty with function pointers. I'm writing a 'Terminal' class, which in turn has a member class 'CommandMap'. The purpose of the CommandMap class is to store a vector/array of functions and the strings that represent them in another vector/array. I want the functions to be called (only) when the class calls them from the vector, but it executed only when I added it to the vector and not when trying to call it.</p>
  
  <p>I tried defining a type for it:</p>
  
  <pre><code>typedef void (*CmdCallback)();
  </code></pre>
  
  <p>I declared a vector to contain them:</p>
  
  <pre><code>vector&lt;string&gt; CmdNames;
  vector&lt;CmdCallback&gt; CmdFuncs;
  </code></pre>
  
  <p>I add them like so:</p>
  
  <pre><code>// Map a new command
  bool CommandMap::Map(string name, CmdCallback func)
  {
      if (!IsNullOrSpace(name) &amp;&amp; func != NULL)
      {
          if (!Exists(name))
          {
              CmdNames.push_back(name);
              CmdFuncs.push_back(func);
              return true;
          }
      }
  
      return false;
  }
  </code></pre>
  
  <p>And I try calling them like this:</p>
  
  <pre><code>// Get a command callback from its identifier
  CmdCallback CommandMap::GetFunc(string name)
  {
      int index = IndexOf(name);
      if (index == -1) return NULL;
      else return CmdFuncs.at(index);
  }
  
  // If the given string is a command indentifier
  // it will invoke the associated callback.
  bool CommandMap::Exec(string input)
  {
      for each (string id in CmdStrings)
      {
          if (input == id)
          {
              CmdCallback cmd;
              cmd = GetFunc(id);
              cmd();
              return true;
          }
      }
  
      return false;
  }
  </code></pre>
  
  <p>I tried using this:</p>
  
  <pre><code>CmdCallback SayHello()
  {
      cout &lt;&lt; "Hello World!" &lt;&lt; endl;
      return NULL; // Forces me to return null, guessing since it's
                   // not 'void' but a 'void' pointer it must return something
  }
  
  int main(int argc, char *argv[])
  {
      App = new Terminal(argc, argv);
      App-&gt;Commands-&gt;Map("say", SayHello);
  
      while (!App-&gt;ExecComplete)
      {
          App-&gt;WaitEnter();
          App-&gt;Commands-&gt;Exec("say");
          App-&gt;WaitEnter();
          App-&gt;ExecComplete = true;
      }
  
      return App-&gt;ExitCode;   
  }
  </code></pre>
  
  <p>This works, at first. The function gets called when I try to <code>Map()</code> it though. And when I <code>Exec()</code> "say", it finds the callback, but when it tries to call it, I get this runtime error, to which I can see no detail other than the option to break or continue. The code it gives me is.</p>
  
  <p>I pretty much want to abandon my method and try a new approach, maybe I'm going the wrong way with the void pointer typedef, and I need to throw a '&amp;' or a '*' somewhere I haven't like in the <code>Map()</code> argument list. Maybe a vector isn't the best way to do this either.</p>
  
  <p>Basically, I am asking how I can I make an array of functions that can (and only) be called by referencing them from the array. I'm terrible with callbacks.</p>