C API函数回调到C ++成员函数code [英] C API function callbacks into C++ member function code

查看:162
本文介绍了C API函数回调到C ++成员函数code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我使用的FMOD API和它确实是一个C API。

So, I'm using the FMOD api and it really is a C api.

这并不是说这是很糟糕或任何东西。它只是它不与C ++ code接口很好。

Not that that's bad or anything. Its just it doesn't interface well with C++ code.

例如,使用

FMOD_Channel_SetCallback( channel, callbackFunc ) ;

它想为 callbackFunc C风格的功能,但我想传递一个类的成员函数。

It wants a C-style function for callbackFunc, but I want to pass it a member function of a class.

我结束了使用Win32伎俩对于这一点,使得成员函数静态的。然后,它可以作为一个回调到FMOD。

I ended up using the Win32 trick for this, making the member function static. It then works as a callback into FMOD.

现在我已经砍除了我的code,使一些成员静态的,只是考虑到FMOD的C-内斯。

Now I have to hack apart my code to make some of the members static, just to account for FMOD's C-ness.

我不知道其可能在FMOD或者如果周围有一个工作回调链接到一个特定的C ++对象的实例成员函数(而不是一个静态函数)。这将是更加顺畅。

I wonder if its possible in FMOD or if there's a work around to link up the callback to a specific C++ object's instance member function (not a static function). It would be much smoother.

推荐答案

您不能直接传递一个成员函数。成员函数的隐含参数这个和C函数没有。

You cannot directly pass a member function. A member function has the implicit parameter this and C functions don't.

您需要创建一个蹦床(不知道回调的签名,所以只是做一些随机这里)。

You'll need to create a trampoline (not sure the signature of the callback, so just doing something random here).

extern "C" int fmod_callback( ... args ...)
{
    return object->member();
}

一个问题是,这是否对象的指针是从哪里来的。我们希望,FMOD给你当你的回调提出,将提供给你一个通用的上下文值(然后你可以在对象指针传递)。

One issue is where does that object pointer come from. Hopefully, fmod gives you a generic context value that will be provided to you when your callback is made (you can then pass in the object pointer).

如果没有,你只需要使它的全球访问它。

If not, you'll just need to make it a global to access it.

这篇关于C API函数回调到C ++成员函数code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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