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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:47:19+00:00 2026-05-16T19:47:19+00:00

First of all, I know there are excellent implementations (Qt, Boost, cpp-event, etc.), but

  • 0

First of all, I know there are excellent implementations (Qt, Boost, cpp-event, etc.), but I ask the question because I want to know how it works !

If I understand correctly, the “event system” uses the Observer pattern : some objects are observing, waiting for something to happen… and some others send signals. All right.

So, let’s say I’ve got my observer class, with this kind of thing :

void Observer::getNotified() 
{
    // Do stuff
}

My question is : how to manage dynamically what kind of stuff should be done ? I’ve seen a lot of people saying specifically not to use functions pointers. For my current need, I can do a switch statement, a enum type, and select different behaviors, but that’s not very satisfying. So, if it’s not functions pointers, how is it done ?

  • 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-05-16T19:47:19+00:00Added an answer on May 16, 2026 at 7:47 pm

    There is a lot of different implementations possible (whatever the language), but the abstract idea is :

    1. have an event type identifier to know wich events have been fired by the observed object (it can be whatever works);
    2. have a list of observers (or several, one by event type?) registered in the registered objects – that suppose that the observers are functors (function pointer or object that act like a function) or are objects with a known interface (with a virtual method to call ion event fired);
    3. when an event is fired (by calling a function on the observed object) we just go through the list of observers registered for the event’s type and pass the event information (id plus maybe data ) to the observer to be processed;

    Here is a simplist implementation :

    enum EventType
    {
       Event_A,
       Event_B,
    };
    
    class Observer // for this example i'll suppose observer inherits this class
    {
        virtual void catchEvent( EventType e ) = 0; // for sake of the example I just need the event type, not the data, but it's often required
    
        virtual ~Observer(){}; // virtual base classes require this to work fine.
    };
    
    class Observed
    {
          typedef std::vector<Observer*> ObserverList; 
          typedef std::map< EventType, ObserverList > ObserversTable;
          ObserversTable m_observers; // we have observers for each event type
    
    public:
    
         void addObserver( Observer& observer, EventType eType ) 
         { m_observers[ eType ].push_back( observer ); } // this is simplist, to say the least...
    
         void sendEvent( EventType event ) // here we send the event, it will be catched by observers
         { // note : often this type of system is "buffered", but here we will process it immediatly as it's a simple example
    
            ObserverList& observers = m_observers[ event ];  
            for ( ObserverList::iterator it = observers.begin(); it != observers.end(); ++it )
            {
                  Observer* observer = *it;
                  observer->catchEvent( event ); // here an observer receive the event
            }
    
         }
    };
    

    It’s really a basic example but that might help you understand the idea.

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

Sidebar

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.