C++ 避免c+中的静态成员函数+;使用C中的回调接口时
我想访问此成员函数中的静态数据。现在,成员函数是静态的,因此我可以将其与用C编写的第三方API一起使用,该API具有用于回调的typdef函数指针。根据下面的信息,为了在我的类的其他非静态成员函数中使用来自以下函数成员的数据,最好的方法是什么。也许有一种方法仍然可以使用这个静态函数,但仍然可以克服无法将静态变量与非静态变量混合的缺点。我的代码按原样工作,但无法访问以下回调函数中的数据C++ 避免c+中的静态成员函数+;使用C中的回调接口时,c++,callback,function-pointers,C++,Callback,Function Pointers,我想访问此成员函数中的静态数据。现在,成员函数是静态的,因此我可以将其与用C编写的第三方API一起使用,该API具有用于回调的typdef函数指针。根据下面的信息,为了在我的类的其他非静态成员函数中使用来自以下函数成员的数据,最好的方法是什么。也许有一种方法仍然可以使用这个静态函数,但仍然可以克服无法将静态变量与非静态变量混合的缺点。我的代码按原样工作,但无法访问以下回调函数中的数据 void TextDetect::vtrCB(vtrTextTrack *track, void *callda
void TextDetect::vtrCB(vtrTextTrack *track, void *calldata) /*acts as a callback*/
{
/*specifically would like to take data from "track" to a deep copy so that I don't loose scope over the data withing that struct */
}
在用C编写的相关API中,我不得不使用以下两行代码:
typedef void (*vtrCallback)(vtrTextTrack *track, void *calldata);
int vtrInitialize(const char *inifile, vtrCallback cb, void *calldata);
这是我的课程的标题:
#include <vtrapi.h>
#include <opencv.hpp>
class TextDetect {
const char * inifile;
vtrImage *vtrimage;
int framecount;
public:
TextDetect();
~TextDetect();
static void vtrCB(vtrTextTrack *track, void *calldata);
int vtrTest(cv::Mat);
bool DrawBox(cv::Mat&);
};
TextDetect::TextDetect() : inifile("vtr.ini")
{
if (vtrInitialize(inifile, vtrCB /*run into problems here*/, NULL) == -1)
std::cout << "Error: Failure to initialize" << std::endl;
vtrimage = new vtrImage;
framecount = 0;
}
void TextDetect::vtrCB(vtrTextTrack *track, void *calldata) /*acts as a callback*/
{
/*specifically would like to take data from "track" to a deep copy so that I don't loose scope over the data withing that struct */
}
#包括
#包括
类文本检测{
const char*ini文件;
vtrImage*vtrImage;
整数帧计数;
公众:
TextDetect();
~TextDetect();
静态void vtrCB(vtrtextrack*track,void*calldata);
内部vtrTest(cv::Mat);
布尔抽屉(cv::Mat&);
};
TextDetect::TextDetect():ini文件(“vtr.ini”)
{
if(vtrInitialize(ini文件,vtrCB/*在此处遇到问题*/,NULL)=-1)
STD::CUT< P>我不确定我理解你的精确情况,但是这里是一个将C++方法封装到C回调API中的标准习惯用法:
/*regular method*/
void TextDetect::vtrCB(vtrTextTrack *track)
{
// do all the real work here
}
/*static method*/
void TextDetect::vtrCB_thunk(vtrTextTrack *track, void *data)
{
static_cast<TextDetect *>(data)->vtrCB(track);
}
您对这样的东西感兴趣吗:?vtrInitialize是一个由第三部分组成的api。当您使用static\u cast(这个)时,这会有什么不同吗
?不应该这样。vtrInitialize
对其第三个参数没有任何作用,它只是将其传递给vtrCB\u thunk
,因此这两个强制转换都在您的代码中。您的帮助非常正确。谢谢。
vtrInitialize(inifile, TextDetect::vtrCB_thunk, static_cast<void *>(this));