0

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()?

unwind
  • 391,730
  • 64
  • 469
  • 606
paultop6
  • 3,691
  • 4
  • 29
  • 37

1 Answers1

2

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.

Community
  • 1
  • 1
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236