使用boost :: C两端code bind()的,它是否行得通呢? [英] Using boost::bind() across C code, will it work?

查看:203
本文介绍了使用boost :: C两端code bind()的,它是否行得通呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用的boost ::绑定(myCallBack函数,这一点,_1,_2) C两端code?

Can I use boost::bind(mycallback, this, _1, _2) across C code?

更新

简短的答案是,升压绑定不返回一个函数指针,它可以在C code被调用,而是一个的仿的(C ++对象,重载()运营商)请参阅下面的回答。

The short answer is no, boost bind does not return a function pointer, which can be called in C code, but a functor (C++ object with overloaded () operator) see answer below.

推荐答案

做你想做的事,最好的方法是创建一个C回调然后调用了boost ::功能,存储在某种类型的用户内存新的。

The best way to do what you want to do is to create a C callback that then calls the boost::function, which is stored in some sort of user memory with new.

例如:

void callFunction(void* data)
{

   boost::function<void(void)> *func = (boost::function<void(void)>* ) (data);
   (*func)();
   delete(func);
}

然后你简单地传递这个回调并设置用户数据(但它是在libev指定)要与新分配的功能的副本。

Then you simply pass this callback and set the user data(however it is specified in libev) to be a copy of your function allocated with new.

这是你如何与libev指定的用户数据:
http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#ASSOCIATING_CUSTOM_DATA_WITH_A_WATCH

This is how you specify user data with libev: http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#ASSOCIATING_CUSTOM_DATA_WITH_A_WATCH

这篇关于使用boost :: C两端code bind()的,它是否行得通呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆