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
  • 378,987
  • 63
  • 458
  • 590
paultop6
  • 3,563
  • 4
  • 28
  • 35

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
  • 95,426
  • 26
  • 188
  • 233