I know 2 ways for desiging an event in C++:
1: Using callbacks:
typedef void (*callback_type)(void);
class my_class
{
public:
my_class(callback_type c)
{
m_callback = c;
}
void raise_event()
{
m_callback();
}
private:
callback_type m_callback;
};
2: Using virtual methods:
class my_class
{
public:
virtual void my_event() = 0;
void raise_event()
{
my_event();
}
};
class main_class : public my_class
{
public:
virtual void my_event()
{
// Handle EVENT.
}
};
Is there any other way or other idea for designing events?
and
What is the best pattern for designing events in ISO C++?
You should use Boost.Signals or Boost.Signals2.
To emulate those, you can use a collection of Boost.Function’s/
std::function‘s.To emulate those, you use type erasure (so the virtual function route) for flexibility.
Note that none of that is too trivial, so you should really try to use an existing solution if possible.