This question has been haunting me for several days. It looks very simple, but it’s very difficult for me to figure it out.
Basically, I want to do something like the async_wait function in the following code snippet
boost::asio::io_services io;
boost::asio::deadline_timer timer(io);
timer.expires_from_now(boost::posix_time::milliseconds(1000));
timer.async_wait(boost::bind(&FunctionName, arg1, arg2, ...)); // How to implement this in my class A
My sample code:
#include <iostream>
#include <string>
//#include <boost/*.hpp> // You can use any boost library if needed
// How to implement this class to take a handler with variable number of arguments?
class A
{
public:
A()
{
}
void Do()
{
// How to call the handler with variable number of arguments?
}
};
void FreeFunctionWithoutArgument()
{
std::cout << "FreeFunctionWithoutArgument is called" << std::endl;
}
void FreeFunctionWithOneArgument(int x)
{
std::cout << "FreeFunctionWithOneArgument is called, x = " << x << std::endl;
}
void FreeFunctionWithTwoArguments(int x, std::string s)
{
std::cout << "FreeFunctionWithTwoArguments is called, x = " << x << ", s =" << s << std::endl;
}
int main()
{
A a;
a.Do(); // Will do different jobs depending on which FreeFunction is passed to the class A
}
P.S.: you can use any boost library if needed, such as boost::bind, boost::function
If you have a C++1x compiler, replace
boost::withstd::.