How to Work with Events in a C++ Class | CodeGuru

How to Work with Events in a C++ Class

Environment: All ANSI C++ compilers If you want to create a C++ class that calls clients’ functions (events), you can do that in two ways. The first way is to call a global function through a function pointer that is a private member of your class. The client has to set that function pointer if […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 18, 2002
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: All ANSI C++ compilers

If you want to create a C++ class that calls clients’ functions (events), you can do that in two ways. The first way is to call a global function through a function pointer that is a private member of your class. The client has to set that function pointer if it wants to handle that event. So, your class has two constructors, one with a function pointer and the other without it. If the client wants to handle an event, it has to create an instance of your class with the constructor that has a function pointer as the argument.

The second way is to call a class member function of the client class through the interface that is provided with your class package. The client has to inherit that interface and implement an event function if it wants to handle that event. So, your class has two constructors, one with an event interface pointer and the other without it. If the client wants to handle an event, it has to create an instance of your class with the constructor that has a function pointer as the argument; for example, “CEvent(this)”. Here is the code for both ways:

Global way:
class ClassWithEvents
{
private:
  int i;
  void (*iChange)(int &);
public:
  ClassWithEvents();
  ClassWithEvents(void (*iChange)(int &));
  void set_i(int);
  int get_i(void);
};
ClassWithEvents::ClassWithEvents()
{
  this->iChange = 0L;
  this->i = 0;
}
ClassWithEvents::ClassWithEvents(void (*iChangeHandler)(int &))
{
  this->iChange = iChangeHandler;
  this->i = 0;
}
void ClassWithEvents::set_i(int iNum)
{
  this->i = iNum;
  if(this->iChange)
  {
    this->iChange(this->i);
  }
}
int ClassWithEvents::get_i(void)
{
  return this->i;
}
void myEvent(int &iNum)
{
  printf(“Value of property ‘i’ = %d.n”, iNum);
}
void main(void)
{
  ClassWithEvents objClassWithEvents(myEvent);
  objClassWithEvents.set_i(4);
}
Class way:
class ClassWithEventsEvents
{
public:
  virtual void iChange(int &) = 0;
};
class ClassWithEvents
{
private:
  int i;
  ClassWithEventsEvents *objClassWithEventsEvents;
public:
  ClassWithEvents();
  ClassWithEvents(ClassWithEventsEvents *);
  void set_i(int);
  int get_i(void);
};
ClassWithEvents::ClassWithEvents()
{
  this->objClassWithEventsEvents = 0L;
  this->i = 0;
}
ClassWithEvents::ClassWithEvents(ClassWithEventsEvents
                                 *objClassWithEventsEventsHandler)
{
  this->objClassWithEventsEvents =
        objClassWithEventsEventsHandler;
  this->i = 0;
}
void ClassWithEvents::set_i(int iNum)
{
  this->i = iNum;
  if(objClassWithEventsEvents)
  {
    objClassWithEventsEvents->iChange(this->i);
  }
}
int ClassWithEvents::get_i(void)
{
  return this->i;
}
class EventHandler : public ClassWithEventsEvents
{
private:
  void iChange(int &iNum)
  {
    printf(“Value of property ‘i’ is = %d.n”, iNum);
  }
public:
  ClassWithEvents *objClassWithEvents;
  EventHandler()
  {
    objClassWithEvents = new ClassWithEvents(this);
  }
  ~EventHandler()
  {
    delete objClassWithEvents;
  }
};
void main(void)
{
  EventHandler objEventHandler;
  objEventHandler.objClassWithEvents->set_i(4);
}

Downloads


Download source – 23 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.