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 7719367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:23:57+00:00 2026-06-01T03:23:57+00:00

I’m trying to make a simple event dispatcher system. To put it simply, I

  • 0

I’m trying to make a simple event dispatcher system. To put it simply, I have event classes that all derive a IEvent interface. I also have an EventManager class with a couple of methods:

void    addListener( int eventID, std::function<void( IEvent *event )> callback );
void    dispatchEvent( int eventID, IEvent *event );

The EventManager has a map of vectors:

std::map<int, std::vector<std::function<void( IEvent * )> > >   m_events;

Any object can then register via the addListener method, giving it a callback, and it will be notified whenever someone calls dispatchEvent with the same event type:

// add an event listener
EventManager::addListener(Event_MouseMove, std::bind(&Obj::callback, this, std::placeholders::_1));

// the callback method
void Obj::callback( IEvent *e )
{
    // here I need to cast e to a MouseEvent *
    // I want to get rid of this.
    MouseEvent *me = (MouseEvent *)e;
}

When calling dispatchEvent, the caller must pass an IEvent derived class, MouseEvent for example:

// dispatching an Event
EventManager::dispatchEvent(Event_MouseMove, new MouseEvent());

My problem has to do with the callback method.
Say you want to listen for a MouseEvent, the object would have a method like such:

void    eventHandler( MouseEvent *event );

The problem is that the callback can not be passed to the addListener method as it is expecting an IEvent * and not a MouseEvent *. To make it work, I just use IEvent types and then the event handler method must cast that IEvent * to the derived type it is expecting; in this case a MouseEvent *.
How can I make it that the addListener method take a pointer to a method that will receive any IEvent derived class ?

As an example:

// add an event listener
EventManager::addListener(Event_MouseMove, std::bind(&Obj::callback, this, std::placeholders::_1));

// the callback method
void Obj::callback( MouseEvent *e ) { ... }

// dispatching an Event
EventManager::dispatchEvent(Event_MouseMove, new MouseEvent());
  • 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-01T03:23:58+00:00Added an answer on June 1, 2026 at 3:23 am

    I don’t think that’s possible, without changing the function parameter of addListener() to having a MouseEvent* parameter (or one derived from MouseEvent) instead of an IEvent* parameter.

    If you have eventHandlers that take pointers to other classes (derived from IEvent) as a parameter, you’d thus need a separate addListener() function for each of these, which probably isn’t what you want.

    Simply passing an IEvent* and then casting it to the appropriate type in the eventHandler (as you’re currently doing) is probably the way to go.

    As an example, Qt actually does something something similar for its eventFilters: http://qt-project.org/doc/qt-4.8/eventsandfilters.html

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is

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.