passing functor as function pointer

后端 未结 10 2263
旧巷少年郎
旧巷少年郎 2020-11-29 03:52

I\'m trying to use a C library in a C++ app and have found my self in the following situation (I know my C, but I\'m fairly new to C++). On the C side I have a collection of

10条回答
  •  悲哀的现实
    2020-11-29 04:44

    A C callback function written in C++ must be declared as an extern "C" function - so using a functor directly is out. You'll need to write some sort of wrapper function to use as that callback and have that wrapper call the functor. Of course, the callback protocol will need to have some way of passing context to the function so it can get to the functor, or the task becomes quite tricky. Most callback schemes have a way to pass context, but I've worked with some brain-dead ones that don't.

    See this answer for some more details (and look in the comments for anecdotal evidence that the callback must be extern "C" and not just a static member function):

    • C++ Using Class Method as a Function Pointer Type

提交回复
热议问题