C API function callbacks into C++ member function code

前端 未结 5 2029
旧巷少年郎
旧巷少年郎 2020-12-10 14:37

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

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

For example, usin

5条回答
  •  执念已碎
    2020-12-10 15:02

    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();
    }
    

    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.

提交回复
热议问题