成员函数指针和回调

发布于 2024-11-14 05:35:55 字数 902 浏览 3 评论 0原文

你好 我试图传递一个回调函数,它是一个成员函数。我知道函数指针和成员函数指针是不同的,并尝试创建一个包装器并使用静态强制转换和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷乱花海 2024-11-21 05:35:55

从您所显示的情况来看,问题很可能是 dSpaceCollide 只需要一个

typedef void (*dNearCallback)(void*,dGeomID,dGeomID)

回调,即具有单个 void* 用户数据指针和两个 < code>dGeomIDs 作为参数。 void* 是您随后传递给 dSpaceCollide 的任何数据。

假设 dSpaceCollide 的定义如下:

void dSpaceCollide(CSpace s, void* user_data, dNearCallback cb){
  // somewhere inside the code it will call your callback:
  cb(user_data, some_other, params);
  // ...
}

然后您可以将回调更改为:

void NearCallback(dGeomID o1, dGeomID o2);
static void StaticNearCallback(void* data, dGeomID o1, dGeomID o2){
  ODEPhysics* self = static_cast<ODEPhysics*>(data);
  self->NearCallback(o1, o2);
}

成员函数中不再需要 void* data 指针,因为 data 仅与静态回调相关:

// assuming you call this inside of your ODEPhysics class
dSpaceCollide(Space, this, &ODEPhysics::StaticNearCallback);
// pass this as user_data

The problem is most likely, from what you show, that dSpaceCollide just wants a

typedef void (*dNearCallback)(void*,dGeomID,dGeomID)

callback, that is, a function with a single void* user-data pointer and the two dGeomIDs as parameters. The void* is whatever data you passed to the dSpaceCollide then.

Assuming dSpaceCollide is defined something like this:

void dSpaceCollide(CSpace s, void* user_data, dNearCallback cb){
  // somewhere inside the code it will call your callback:
  cb(user_data, some_other, params);
  // ...
}

You can then change the callbacks to this:

void NearCallback(dGeomID o1, dGeomID o2);
static void StaticNearCallback(void* data, dGeomID o1, dGeomID o2){
  ODEPhysics* self = static_cast<ODEPhysics*>(data);
  self->NearCallback(o1, o2);
}

No more need for the void* data pointer in the member function, as that data is only relevant for the static callback:

// assuming you call this inside of your ODEPhysics class
dSpaceCollide(Space, this, &ODEPhysics::StaticNearCallback);
// pass this as user_data
痕至 2024-11-21 05:35:55

我将在这里猜测,并假设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 no void* userPtr passed to your callback. The type system catches this when you try to use StaticNearCallback, which does expect a void* userPtr.

Underlying this problem might be a more fundamental misunderstanding. Why does your StaticNearCallback have two untyped void* pointers? What are you doing with void* data in NearCallback? Remember, C++ methods have a (typed) this pointer, so you rarely need an untyped void* data.

离线来电— 2024-11-21 05:35:55

看看 boost::function (也许boost::bind)。它们通常可以让您的生活变得更加轻松。

Have a look at boost::function (and maybe boost::bind). They can often make your life a lot easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文