boost::使用成员函数绑定回调的帮助
boost::bind help for callback with member function
你好,我是一名学生,正在编写一个使用成员函数回调的程序。我偶然发现了绑定的用法,这正是我所需要的。我只是很难让它发挥作用。
以下是的相关代码和编译错误
// this is the API function to register callback
void register_callback_datapoint(void(*)(datapoint_t *datapoint) cb_datapoint )
// this function is my callback
void datapoint_update(datapoint_t* datapoint);
// this code is called in the aggregateThread class
boost::function<void(datapoint_t*)> f;
f = bind(&aggregateThread::datapoint_update, this, std::tr1::placeholders::_1);
register_callback_datapoint(f);
// here is the compile error
cannot convert ‘boost::function<void(datapoint_opaque_t*)>’ to ‘void (*)(datapoint_t*)
{aka void (*)(datapoint_opaque_t*)}’ for argument ‘1’ to ‘void
register_callback_datapoint(void (*)(datapoint_t*))’
有人能帮我做这个吗?谢谢
首先,我很惊讶您没有收到void register_callback_datapoint(void(*)(datapoint_t *datapoint) cb_datapoint )
的错误。正确的语法应该是void register_callback_datapoint(void(*cb_datapoint)(datapoint_t *datapoint));
,用于声明函数指针作为参数。
但是,问题是您试图传递一个boost::function
,它是一个函数对象,不能隐式转换为指向register_callback_datapoint
的函数指针。您需要将参数更改为boost::function
或使其成为模板。
void register_callback_datapoint(boost::function<void(datapoint_opaque_t*)> f);
或
template <typename Func>
void register_callback_datapoint(Func f);
此外,我刚刚注意到这一点,但您的示例和编译错误不匹配。一个说datapoint_opaque_t*
,另一个说是datapoint_t*
,它们是不同的名称。我不知道这是否会成为一个问题。
相关文章:
- 架构决策:返回std::future还是提供回调
- 正在为Xtensa simcall函数编写回调函数
- 如何在C++中使用非静态成员函数作为回调函数
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 用于在回调中调用解析器的设计模式
- 如何使用C++对象的成员函数作为 C 样式回调?
- Java从C++回调到C++回调
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 从不同的 cpp 调用回调函数会导致bad_function_call
- pcap_handler回调仅在使用 NPCAP v0.9991 时包含空数据包
- 不带轮询的 SDL2 事件回调
- C++存储带有可变参数的回调
- 如何使用 Node-addon-API 实现 node-nan 回调
- 处理影响跨不同线程共享对象的定时回调的最佳方法是什么?
- 访问类C++ C 样式回调
- 处理类内的回调时,必须调用对非静态成员函数的引用
- 如果 C 函数仍然可以间接执行(通过回调函数),那么将它声明为静态函数是否是一种不好的做法?
- 在C++中实现回调
- boost::使用成员函数绑定回调的帮助