我需要在 C++
代码中使用 g_idle_add()
,其中 GSourceFunc
是另一个类函数并且还必须传递一些参数.仅在 C 代码中看到它的使用。所以可能是我没有把事情做好
虽然在 C 代码中使用 g_idle_add()
很简单
C程序
g_idle_add ((GSourceFunc) functionA, someData);
其中 functionA 是在该 C 程序文件范围内定义的函数,someData 是结构
C++程序
g_idle_add ((GSourceFunc) (mObjOfAnotherClass->functionB (* p_SomeVariable)), NULL)
此外,考虑我必须将多个参数传递给函数 B 的场景
这里的区别在于从 g_idle_add 调用的函数的范围。我可以像上面示例 C++ 代码中那样调用 g_idle_add()
最佳答案
指向函数的指针不同于指向类方法的指针。如果你想使用指向方法的指针,你必须有一个类的对象。您应该将您的方法包装在 C 函数中。例如:
extern "C"
{
void myCFunction(void *p_SomeVariable)
{
// store your object where you want
static AnotherClass mObjOfAnotherClass;
mObjOfAnotherClass.functionB(*p_SomeVariable);
}
}
然后:
g_idle_add ((GSourceFunc) myCFunction, p_SomeVariable);
关于c - 在 C++ 中使用 g_idle_add() 时出错,在 C 中同样有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18185733/