Posted November 7, 2012 by Victor in C/C++ Programming

Event Management in C++


In simple terms, events refer to inputs and outputs resulting from user actions such as mouse clicks and key presses. Events can also be messages from other programs instructing the main program to perform a given action. Event management in C++ is divided into two:

  1. Event Selection: Also known as event detection, this is the step in which your program will detect the performance of a specified event.
  2. Event Handling: In this section, once the event has been detected, the program processes the event and gives it the needed resources for the correct output.

In C++, events management is often achieved using interrupts as opposed to running a single loop. Here are the steps to follow when programming for events management in C++:

  1. Code for event handlers: Event handlers are a series of methods that are coded so as to manage the manner in which the main program will react to given events calls. For instance, an event handler may be put in place for the mouse click event such that when the left mouse button is clicked, a routine call may be triggered such that a new window will be opened. Event handlers can range from easy to code functions which perform simple calculations to complex functions that process a series of looped requests.
  2. Associating Event Handlers with actual events: There are many events and once you code the event handlers, you will need to associate them with the actual events so that your C++ program acts accordingly and the correct C++ function is called when the said event occurs. Many at times, associating event handlers with actual events in C++ revolves around creating conditional statements that specify the direction your program will take when a given event is performed.
  3. Create the main Loop: Once you have coded the event handler and associated it with the actual event, you will need to have a way in which to check the occurrence of the event. The main loop is the function created for the purpose of checking if an event has occurred and then matching the event with the right event handler.

C++ Event Management Using Call Back Functions

There are still other ways of creating C++ events management criteria.  For instance, you can use function pointers and call back functions which will handle the given event.

  1. The most common type of event management in C++ is event management for Input and Output related events.
  2. This type of C++ event management is achieved through call back functions. Call back functions are simply functions passed to the routine and called at some point by the routine to which it was passed.
  3. Using call back functions is simple and revolves around creating a function that handles a given event, placing your function in a routine and using the right conditional codes such that when the right event for the function is needed, the function is called to process the event.

In conclusion, even though C++ is not the best programming language for handling events, it can be used for event management if you really know how to deal with functions and pointers. For more complex events, it is best to use other programming languages that provide GUI such as Visual basic.