After some research on internet for an efficient way to implement events in C++, I found the following methods
I am confused on advantages and disadvantages of each of these and when to use anyone of this. Which is the best method and why? Is there any other solutions which are better than the ones listed?
FWIW, my vote would be for Boost Signals any day.
Boost ensures portability. Of course it integrates nicely with e.g. Boost Asio, Functional, Bind, etc.
Update:
Signals2
This documentation describes a thread-safe variant of the original Boost.Signals library. There have been some changes to the interface to support thread-safety, mostly with respect to automatic connection management. [....]
Boost ensures portability. Of course it integrates nicely with e.g. Boost Asio, Functional, Bind, etc.
boost::signals2::signal sig;
sig.connect(&print_sum);
sig.connect(&print_product);
sig.connect(&print_difference);
sig.connect(&print_quotient);
sig(5., 3.);
This program will print out the following:
The sum is 8
The product is 15
The difference is 2
The quotient is 1.66667
sample actions:
void print_sum(float x, float y)
{
std::cout << "The sum is " << x+y << std::endl;
}
void print_product(float x, float y)
{
std::cout << "The product is " << x*y << std::endl;
}
void print_difference(float x, float y)
{
std::cout << "The difference is " << x-y << std::endl;
}
void print_quotient(float x, float y)
{
std::cout << "The quotient is " << x/y << std::endl;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With