g_time_out add 可以被赋予一个类的成员函数吗?

发布于 2024-08-31 04:11:20 字数 108 浏览 1 评论 0原文

问题就在标题中,如果我想通过 g_timeout_add() 使用一个函数,但这个函数是一个类成员函数,有什么方法可以将它与 一起使用>g_timeout_add()

The question is simply in the title, if I have a function I want to use via a g_timeout_add(), but this function is a class member function, is there any way I can use it with g_timeout_add()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2024-09-07 04:11:20

您需要使用蹦床函数,例如:

extern "C" gboolean trampoline(gpointer data) {
    static_cast<MyClass*>(data)->mem_fun();
}

// ...
MyClass c = /* ... */;
g_timeout_add(/*...*/, static_cast<gpointer>(&c));

请参阅 这个问题说明如果您想编写可移植代码,为什么应该使用自由函数。

You need to use a trampoline function, e.g.:

extern "C" gboolean trampoline(gpointer data) {
    static_cast<MyClass*>(data)->mem_fun();
}

// ...
MyClass c = /* ... */;
g_timeout_add(/*...*/, static_cast<gpointer>(&c));

See this question on why you should use free functions if you want to write portable code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文