成员函数指针和回调
你好 我试图传递一个回调函数,它是一个成员函数。我知道函数指针和成员函数指针是不同的,并尝试创建一个包装器并使用静态强制转换和 void 指针。不幸的是,我遗漏了一些东西,因为我的代码在编译时产生错误
Error 16 error C2664: 'dSpaceCollide' : cannot convert parameter 3 from 'int (__cdecl *)(void *,void *,dGeomID,dGeomID)' to 'dNearCallback (__cdecl *)'
我的代码.....
class ODEPhysics
头文件
void NearCallback (void* data, dGeomID o1, dGeomID o2);
static int StaticNearCallback(void* data, void* userPtr, dGeomID o1, dGeomID o2);
.cpp file
void ODEPhysics::NearCallback (void* data, dGeomID o1, dGeomID o2){.........}
void ODEPhysics::StaticNearCallback(void* data , void* userPtr, dGeomID o1, dGeomID o2)
{
static_cast<ODEPhysics*>(userPtr)->NearCallback( data, o1, o2);
}
dSpaceCollide (Space, 0, &ODEPhysics::StaticNearCallback);
如果有人能澄清我做错了什么以及为什么会非常感激。
弗雷德
Hi
I am trying to pass a callback which is a member function. I understand that a function pointer and a member function pointer are different and have tried to create a wrapper and use a static cast and the void pointer. Unfortunately I am missing something as my code produces an error at compile
Error 16 error C2664: 'dSpaceCollide' : cannot convert parameter 3 from 'int (__cdecl *)(void *,void *,dGeomID,dGeomID)' to 'dNearCallback (__cdecl *)'
My code.....
class ODEPhysics
header file
void NearCallback (void* data, dGeomID o1, dGeomID o2);
static int StaticNearCallback(void* data, void* userPtr, dGeomID o1, dGeomID o2);
.cpp file
void ODEPhysics::NearCallback (void* data, dGeomID o1, dGeomID o2){.........}
void ODEPhysics::StaticNearCallback(void* data , void* userPtr, dGeomID o1, dGeomID o2)
{
static_cast<ODEPhysics*>(userPtr)->NearCallback( data, o1, o2);
}
dSpaceCollide (Space, 0, &ODEPhysics::StaticNearCallback);
If anyone could clarify what I am doing wrong and why it would be greatly appreciated.
Fred
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您所显示的情况来看,问题很可能是
dSpaceCollide
只需要一个回调,即具有单个
void*
用户数据指针和两个 < code>dGeomIDs 作为参数。void*
是您随后传递给dSpaceCollide
的任何数据。假设 dSpaceCollide 的定义如下:
然后您可以将回调更改为:
成员函数中不再需要 void* data 指针,因为
data
仅与静态回调相关:The problem is most likely, from what you show, that
dSpaceCollide
just wants acallback, that is, a function with a single
void*
user-data pointer and the twodGeomID
s as parameters. Thevoid*
is whatever data you passed to thedSpaceCollide
then.Assuming
dSpaceCollide
is defined something like this:You can then change the callbacks to this:
No more need for the
void* data
pointer in the member function, as thatdata
is only relevant for the static callback:我将在这里猜测,并假设
typedef void dNearCallback(void *data, dGeomID o1, dGeomID o2)
。也就是说,没有void* userPtr
传递给您的回调。当您尝试使用StaticNearCallback
时,类型系统会捕获此问题,而确实需要一个void* userPtr
。这个问题的背后可能是一个更根本的误解。为什么您的
StaticNearCallback
有两个非类型化void*
指针?您在NearCallback
中使用void* data
做什么?请记住,C++ 方法有一个(类型化)this
指针,因此您很少需要非类型化的void* 数据
。I'm going to guess here, and assume
typedef void dNearCallback(void *data, dGeomID o1, dGeomID o2)
. That is to say, there's novoid* userPtr
passed to your callback. The type system catches this when you try to useStaticNearCallback
, which does expect avoid* userPtr
.Underlying this problem might be a more fundamental misunderstanding. Why does your
StaticNearCallback
have two untypedvoid*
pointers? What are you doing withvoid* data
inNearCallback
? Remember, C++ methods have a (typed)this
pointer, so you rarely need an untypedvoid* data
.看看 boost::function (也许boost::bind)。它们通常可以让您的生活变得更加轻松。
Have a look at boost::function (and maybe boost::bind). They can often make your life a lot easier.