我想在C++中做C包装。C++类成员函数C结构函数指针
但我不能将类内的函数转换为结构函数指针。
不应将myCallback函数用作静态函数,因为它必须被覆盖。
下面是示例代码:
#include <iostream>
extern "C" {
typedef struct _client_config
{
void (*on_connect)(void *client, int result);
} client_config;
}
class Parent
{
public:
Parent() {};
~Parent() {};
virtual void myCallback(void* hello, int world) = 0;
};
class Child : public Parent
{
public:
Child() {};
~Child() {};
void myCallback(void* hello, int world)
{
printf("Hello World! \n");
}
};
int main(void)
{
Parent* myClass = new Child();
client_config* myClientConfig = (client_config *)malloc(sizeof(client_config));
myClientConfig->on_connect = myClass->myCallback;
return 0;
}
这是在编译过程中,编译器说的话:
Error 1 error C3867: 'Parent::myCallback': function call missing argument list; use '&Parent::myCallback' to create a pointer to member
Error 2 error C2440: '=' : cannot convert from 'void (__thiscall Parent::*)(void *,int)' to 'void (__cdecl *)(void *,int)'
任何人都可以指出绑定一个类的成员函数的结构函数指针的方式或我在这里做错了什么?