Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8621547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:50:44+00:00 2026-06-12T06:50:44+00:00

I am implementing a simple event system in c++. the system is designed to

  • 0

I am implementing a simple event system in c++. the system is designed to identify events based on a string (the name of the event), and then call a list of callback functions when the event is fired. here is a simple blueprint:

class EventManager:
    public:
        register_event(string name) // creates a new entry in the event table
        register_listener(string name, callback) // adds the callback to name's entry in the event table
        fire_event(string name// executes all functions in the event table entry for name
    private:
        hashmap<string, vector<function>> //the event table

What I am currently struggling with is how to create a hashmap of strings to vectors of functions, and then loop through those functions to execute them. you can assume that every callback knows it’s own function types, so every callback will have arguments (void* userdata, ...) and I will handle managing the va_list in the callback.

if someone can give me a quick snippet that shows how to create the hashmap, and one for how to loop through calling the functions, that would be useful.

EDIT Using Useless’s answer, I now get the following errors:

EventManager.h

#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef unordered_map<string, vector<function<void()>>> CallbackMap;

class EventManager{
public:
    EventManager(){
        callbacks = CallbackMap();
    }

    void EventManager::RegisterEvent(string const& name);
    void EventManager::RegisterListener(string const &name, function<void()> callback);
    void EventManager::FireEvent(string name);
private:
    CallbackMap callbacks;
};

EventManager.cpp

#include "EventManager.h"
#include <string>

using namespace std;

void EventManager::RegisterEvent(string const& name){
    callbacks[name] = NULL;
}

void EventManager::RegisterListener(string const &name, function<void()> callback)
{
    callbacks[name].push_back(callback);
}

bool EventManager::FireEvent(string name){
    auto event_callbacks = callbacks.find(event_name);
    if (event_callbacks == callbacks.end()){
        return false; // ?
    }

    // or std::for_each
    for (auto cb = event_callbacks->second.begin();
         cb != event_callbacks->second.end(); ++cb)
    {
        (*cb)();
    }
    return true;
}

terminal

$ g++ EventManager.cpp -std=c++0x
In file included from EventManager.cpp:1:0:
EventManager.h:7:38: error: ‘function’ was not declared in this scope
EventManager.h:7:52: error: template argument 1 is invalid
EventManager.h:7:52: error: template argument 2 is invalid
EventManager.h:7:53: error: template argument 2 is invalid
EventManager.h:7:53: error: template argument 5 is invalid
EventManager.h:7:55: error: expected unqualified-id before ‘>’ token
EventManager.h:11:5: error: ‘CallbackMap’ does not name a type
EventManager.h:18:47: error: ‘function’ has not been declared
EventManager.h:18:55: error: expected ‘,’ or ‘...’ before ‘<’ token
EventManager.h: In constructor ‘EventManager::EventManager()’:
EventManager.h:14:9: error: ‘callbacks’ was not declared in this scope
EventManager.h:14:33: error: ‘CallbackMap’ was not declared in this scope
EventManager.cpp: In member function ‘void EventManager::RegisterEvent(const string&)’:
EventManager.cpp:7:5: error: ‘callbacks’ was not declared in this scope
EventManager.cpp: At global scope:
EventManager.cpp:10:57: error: ‘function’ has not been declared
EventManager.cpp:10:65: error: expected ‘,’ or ‘...’ before ‘<’ token
EventManager.cpp: In member function ‘void EventManager::RegisterListener(const string&, int)’:
EventManager.cpp:12:5: error: ‘callbacks’ was not declared in this scope
EventManager.cpp:12:31: error: ‘callback’ was not declared in this scope
EventManager.cpp: At global scope:
EventManager.cpp:15:6: error: prototype for ‘bool EventManager::FireEvent(std::string)’ does not match any in class ‘EventManager’
EventManager.h:19:10: error: candidate is: void EventManager::FireEvent(std::string)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:50:45+00:00Added an answer on June 12, 2026 at 6:50 am

    a hashmap of strings to vectors of functions …

    typedef unordered_map<string, vector<function<void()>>> CallbackMap;
    

    the words were all there. Note you could use unordered_multimap instead of a hashmap of vectors, and the function type reflects your callback interface (you may want function<void(Event*)> or something, for example).

    As Ram pointed out in the comment, map and multimap are the equivalent tree-based associative containers if you don’t specifically need a hash (or don’t have C++11, although you can also use boost::unordered_map).

    … and one for how to loop through calling the functions …

    bool EventManager::fire_event(string const& event_name)
    {
        auto event_callbacks = callbacks.find(event_name);
        if (event_callbacks == callbacks.end()) return false; // ?
    
        // or std::for_each
        for (auto cb = event_callbacks->second.begin();
             cb != event_callbacks->second.end(); ++cb)
        {
            (*cb)();
        }
        return true;
    }
    

    Oh, and registering is as simple as:

    void EventManager::register_listener(string const &name,
                                         function<void()> callback)
    {
        callbacks[name].push_back(callback);
    }
    

    (I’m letting it create the event entry lazily and on-demand).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a event system: Various pieces of code will post events to a
I meet are having difficulty in implementing a simple event OnCheckedChanged for control checkBox
I have tried to find out the working solution for implementing simple events booking
I'm implementing a simple camera system in OpenGL. I set up gluPerspective under the
When implementing Domain events should the event handlers be only used for purely domain
My question is simple, where's the onUp event when implementing a GestureListener ? I
I am implementing simple thread that passes messages to main UI thread to make
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
I am implementing a simple application for expenses reporting.The application will use GAE. In
I'm implementing a simple chat in .NET using Rx on the basis of this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.