且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何传递一个类成员函数作为回调?

更新时间:2023-11-21 09:50:28

由于你现在显示你的init函数接受一个非成员函数。所以做这样:

As you now showed your init function accepts a non-member function. so do it like this:

static void Callback(int other_arg, void * this_pointer) {
    CLoggersInfra * self = static_cast<CLoggersInfra*>(this_pointer);
    self->RedundencyManagerCallBack(other_arg);
}

并调用Init与

m_cRedundencyManager->Init(&CLoggersInfra::Callback, this);

这样做是因为指向静态成员函数的函数指针不是成员函数指针,处理就像只是一个***功能的指针。

That works because a function pointer to a static member function is not a member function pointer and can thus be handled like just a pointer to a free function.