Map of function pointers

Posted on 2011-10-31
Last Modified: 2012-08-14
Hi Experts,

I need to store some callback functions and associated int values in a map.  Basically, I want to notify the registered listeners of events.

The callback functions will be in the format ( C style function )
void callback(Event *evt) { }

Could you please let me know how to do this in C++?

Question by:ambuli
    LVL 86

    Accepted Solution

    Well, that could basically be like
    #include <map>
    using namespace std;
    typedef void (*event_callback)(Event *evt);
    typdef unsigned ine event_type;
    map<event_type,event_callback> mapEvents;
    // ...
    #define SOME_EVENT 1
    void do_some_event(Event *evt) { }
    // ...
    int main () {
      pair<map<event_type,event_callback>::iterator,bool> p = mapEventss.insert(map<event_type,event_callback>::value_type(SEOME_EVENT,do_some_event));
      // ...
      event_callback cb;
      map<event_type,event_callback>::iterator i = mapEvents.find(SOME_EVENT);
      if (i != mapEvents.end()) cb = *i;
      Event e;
      (*cb)(&e); // do the call
      return 0;

    Open in new window


    Author Comment

    Thank you JKR.
    LVL 86

    Expert Comment

    You're most welcome - just saw one typo and some need for more explanation in the same line:
    pair<map<event_type,event_callback>::iterator,bool> p = mapEvents.insert(map<event_type,event_callback>::value_type(SEOME_EVENT,do_some_event));
      // 'p' can be used to check if a handler already existd:
      if (!p.second) throw("An event handler for this type already exists in the map!");

    Open in new window


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    why "." vs "->" 23 112
    Load and store *.pnm image file 1 61
    Excel/Word Add-in in what language? 5 88
    computer science syllabus 3 41
    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now