Cã®ã³ã¼ã«ããã¯é¢æ°ãC++ã®ã¡ã³ãé¢æ°ã«ãã¤ã³ãã£ã³ã°ããæ¹æ³
ãã¨ãã° libevent ã®ãã㪠C è¨èªã§ããããã¤ãã³ãé§ååã®ã©ã¤ãã©ãªã C++ ãã使ã£ã¦ããã¨ãC++ ã®ã¡ã³ãé¢æ°ãã³ã¼ã«ããã¯ã¨ãã¦ã»ããã§ããããããããã¨ãå¤ãã§ãããã以ä¸ã®ããã«ããã°ã§ãã¾ãã
ãã¨ãã°ãã³ã¼ã«ããã¯é¢æ°ãã»ããããé¢æ°ã®åãã
void set_foo_callback(void (*)(void* cb_arg), void* cb_arg);
ãªãã以ä¸ã®ããã«ã¯ã©ã¹ã¨ã¡ã³ãé¢æ°ãå¼æ°ã«ã¨ããã³ãã¬ã¼ãé¢æ°ãå®ç¾©ãã
template <typename T, void (T::*FUNC)()> void to_foo_callback(void* cb_arg) { T* obj = reinterpret_cast<T*>(cb_arg); (obj->*FUNC)(); }
以ä¸ã®ããã«åã渡ãã°ããã
class K { public: K() { set_foo_callback(to_foo_callback<K, &K::on_foo>, this); } protected: void on_foo() { ... } };
ã¡ã³ãé¢æ°ãå¼ã³åºãã©ããã¼é¢æ°ããã¡ãã¡æ¸ããªãã¦ããã®ã§ããããã
åä¸ç´ã® GCC ã ã¨é¢æ°ãã¤ã³ã¿ããã³ãã¬ã¼ãå¼æ°ã«æ¸¡ããªãã£ãã¨æããã§ãããæè¿ã¯ã§ãããã§ããã