使用多个参数将成员功能指针转换为标准C函数
Convert a member function pointer to standard C function with multiple arguments
我正在尝试将成员函数指针转换为标准C功能指针而无需成功。
我尝试了不同的方法,但我想念一些东西。
我的问题是我需要调用库的函数,该函数以参数为函数:
void setFunction(void(*cbfun)(float*,int,int,int,int)){ ... }
以这种方式在课堂内:
class base_t {
public:
void setCallback(){
setFunction(&_callback);
}
private:
void _callback(float * a, int b, int c, int d, int e) { ... }
};
不幸的是,_callback((函数不能静态。
我也尝试使用std :: bind但没有财富。
有什么办法可以将成员传递到功能?
我正在尝试将成员功能指针转换为标准C功能指针而无需成功。
简短答案:您不能。
更长的答案:创建一个包装函数,以用作C功能指针,并从中调用成员函数。请记住,您将需要一个对象才能使该成员函数调用。
这是一个示例:
void setFunction(void(*cbfun)(float*,int,int,int,int)){ ... }
class base_t;
base_t* current_base_t = nullptr;
extern "C" void callback_wrapper(float * a, int b, int c, int d, int e);
class base_t {
public:
void setCallback(){
current_base_t = this;
setFunction(&callback_wrapper);
}
private:
void _callback(float * a, int b, int c, int d, int e) { ... }
};
void callback_wrapper(float * a, int b, int c, int d, int e)
{
if ( current_base_t != nullptr )
{
current_base_t->_callback(a, b, c, d, e);
}
}
相关文章:
- 转换指针引用的字符串
- 无法向上转换指针到指针参数
- 混合转换指针和引用
- 堆栈对象的强制转换指针
- 基类到派生模板类的强制转换指针,而不知道类型
- 删除类型转换指针的最佳方法
- 管理到本机值类转换:强制转换指针是否安全?
- 转换指针类型
- 为什么函数不能正确强制转换指针(从基类到派生类)
- 当我们递增下面的类型转换指针时会发生什么?
- 类型强制转换指针构造函数调用
- 如何转换(指针向量)-->(指向指针数组的指针)
- 如何从类功能转换指针
- 在C++对象中:我应该使用父类强制转换指针,还是应该使用实际类本身进行强制转换
- Delphi中的类型转换指针添加
- C++分段错误(核心转储)错误 - 强制转换指针/将函数值返回到线程
- 以C++和运算符优先级键入指向数组成员的强制转换指针
- C++动态强制转换指针的内存清理
- 转换指针和引用的好处
- 在c++中转换指针