Map Like Structure to store functions

I was developing an application for my arduino where I needed to be able to create a key value pair where the key is some identifier supplied by the user, and it’s associated value is a member function ptr, so I came up with this bit of code found here, keep in mind it was something I just put together quickly, so be cautious if you are going to use it.

#ifndef FuncMap_h
#define FuncMap_h

template <typename T, class C>
class FuncMap
{
private:
  struct ListNode
  {
    T info;
    ListNode * next;
    void(C::*fn)();
    ListNode(T newInfo, ListNode * newNext, void(C::*newfn)())
      :info(newInfo), next(newNext), fn(newfn)
    {

    }
  };

  C * instance;
  ListNode * head;
  void (C::*execute(T * key))()
  {
    for (ListNode *p = head; p != 0; p = p->next)
    {
      if (p->info == *key)
      {
        return p->fn;
      }
    }
  }

public:
  FuncMap(C *newInstance = 0) :head(0), instance(newInstance) { };
  ~FuncMap()
  {
    ListNode * temp;
    for (ListNode * p = head; p != 0; )
    {
      temp = p;
      p = p->next;
      delete temp;
    }
    delete instance;
  }
  
  void insert(T key, void(C::*fn)())
  {
    head = new ListNode(key, head, fn);
  }
  
  void execFunc(T * key)
  {
    void (C::*fn)() = execute(key);
    (instance->*fn)();
  }
};
#endif

There is no error checking implemented and is currently set up for only void functions which take zero arguments, but that can easily be remedied by adding another template parameter. All this is, is a linked list of function pointers.

To use this is relatively simple.

  1. Declare a FuncMap
//myheader.h
FuncMap<const String, MYCLASS> * ATCommands

//mycode.cpp
 ATCommands = new FuncMap<const String, MYCLASS>(this);

//2. add a new key value pair
ATCommands->insert("ATZ", &MYCLASS::onPowerUp);

//3. Then when you want to call that function:
void MYCLASS::handleCommand (const String & cmd)
{
  if (isATCommand(cmd)) 
    ATCommands->execFunc(&cmd);
    ...
}

I am sure there is a much better way, perhaps a more obvious solution, but this seemed fun, and I learned about function pointers.