title: Store Function Pointers to any Member Function
body: <h2>My Event Manager</h2>
  
  <p>For a event manager I need to store many pointers to functions in a vector to call them when the event is triggered. (I will provide the source code of the EventFunction helper class at the end of this question.)</p>
  
  <pre><code>// an event is defined by a string name and a number
  typedef pair&lt;string, int&gt; EventKey;
  
  // EventFunction holds a pointer to a listener function with or without data parameter
  typedef unordered_map&lt;EventKey, vector&lt;EventFunction&gt;&gt; ListEvent;
  
  // stores all events and their listeners
  ListEvent List;
  </code></pre>
  
  <p>Registering an listener could be done by calling the first or the second function, depending on if you want receive additional data or not. (This code is from my event manager class.)</p>
  
  <pre><code>public:
    typedef void (*EventFunctionPointer)();
    typedef void (*EventFunctionPointerData)(void* Data);
  
    // let components register for events by functions with or without data parameter,
    // internally simple create a EventFunction object and call the private function
    void ManagerEvent::Listen(EventFunctionPointer Function, string Name, int State);
    void ManagerEvent::Listen(EventFunctionPointerData Function, string Name, int State);
  
  private:
    void ManagerEvent::Listen(EventFunction Function, string Name, int State)
    {
      EventKey Key(Name, State);
      List[Key].push_back(Function);
    }  
  </code></pre>
  
  <h2>Member Function Pointers</h2>
  
  <p>That code doesn't work because I store function pointers but not member function pointers in my List. All these pointers should be member function pointers because a component like <code>ComponentSound</code> will listen to the event <code>"PlayerLevelup"</code> with on of its member functions <code>ComponentSound::PlayerLevelup</code> to play a nice sound if the event is triggered.</p>
  
  <p>A member function pointer in C++ looks like this.</p>
  
  <pre><code>// ReturnType (Class::*MemberFunction)(Parameters);
  void (ComponentSound::*PlayerLevelup)();
  </code></pre>
  
  <p>The problem is, any component class should be able to listen for events, but storing the member function pointers in the event manager requires me to specify the listening class. As you can see in the example, I need to specify <code>ComponentSound</code> but the event manager should simply have a vector of member function pointers to any class.</p>
  
  <h2>Question</h2>
  
  <p>An Answer to one of these question would help me a lot.</p>
  
  <ul>
  <li>How can I store function pointers to any member function in a vector in my event manager? (Maybe it helps that all the listening functions are inherited from one abstract class <code>Component</code>.)</li>
  <li>How can I design my event manager in another way to reach the aimed functionality? (I want to use string and int keys for messages.)</li>
  </ul>
  
  <p>I tried to keep my question general but if you need more informations or code please comment.</p>
  
  <h2>Assignments</h2>
  
  <p>In my vector of member function pointers I use EventFunction instead of only a pointer to provide two message types. One with, and one without a data parameter.</p>
  
  <pre><code>class EventFunction
  {
  private: EventFunctionPointer Pointer; EventFunctionPointerData PointerData; bool Data;
  public:
      EventFunction(EventFunctionPointer Pointer) : Pointer(Pointer), PointerData(NULL), Data(false) { }
      EventFunction(EventFunctionPointerData PointerData) : PointerData(PointerData), Pointer(NULL), Data(true) { }
      EventFunctionPointer GetFunction() { return Pointer; }
      EventFunctionPointerData GetFunctionData() { return PointerData; } bool IsData() { return Data; }
      void Call(void* Data = NULL){ if(this-&gt;Data) PointerData(Data); else Pointer(); }
  };
  </code></pre>