Passing pointer-to-member-function as pointer-to-function

前端 未结 1 1074
醉话见心
醉话见心 2021-01-19 02:43

So here\'s the situation: I\'m using C++, SDL and GLConsole in conjunction. I have a class, SDLGame, which has the Init(), Loop(),

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-19 03:35

    If the only interface you have is that function pointer, then you're screwed.

    A member function needs a this pointer to be called, and if you have no way of passing that, you're out of luck (I guess the std::vector* args pointer is what you get passed from the library).

    In other words, even though that library uses C++ containers, it's not a good C++ library, because it relies on free functions for callbacks. A good C++ library would use boost::function or something similar, or would at the very least let you pass a void* user_data pointer that gets passed through to your callback. If you had that, you could pass the this pointer of your class, cast it back inside the callback, and call the appropriate member function.

    0 讨论(0)
提交回复
热议问题