Container for pointers to member functions with different arguments

前端 未结 3 513
南方客
南方客 2021-01-17 05:41

I am looking everywhere (Modern C++ design & co) but I can\'t find a nice way to store a set of callbacks that accept different arguments and operate on different classe

3条回答
  •  深忆病人
    2021-01-17 06:24

    Since your callbacks get deferred including their provided arguments, the real signature will be void(), i.e. the Clock object won't provide arguments on its own and has no need to evaluate the results. So generally you will want to bind member function pointers (or other function pointers) together with the needed arguments and pass the result (a function object) to the clock. This is where boost::bind/std::tr1::bind and boost::function/std::function come in - or C++11 lambdas:

    Clock::defer(time, boost::bind(&Class1::action1, this, arg1, arg2));
    //C++11 lambda:
    Clock::defer(time, [=](){action1(arg1, arg2);} );
    

    But what you are doing is already done - take a look at Boost.Asio timers:

    boost::asio::basic_deadline_timer timer(ioService, expiryTime);
    timer.async_wait([=](){action1(arg1, arg2);} //perform action1 when the timer is done
    

提交回复
热议问题