0

I need to use g_idle_add() in a C++ code, where a GSourceFunc is another class function and have to pass some arguments also. Have seen its use in C code only. So may be I am not getting the things right

While using g_idle_add() in a C code is straight forward

C program

g_idle_add ((GSourceFunc) functionA, someData);

where functionA is a function defined in that C program file scope and someData is a structure

C++ program

g_idle_add ((GSourceFunc) (mObjOfAnotherClass->functionB (* p_SomeVariable)), NULL)

Also, consider the scenario where I have to pass more than one arguments to functionB

The difference here is about the scope of functions called from g_idle_add. Can I call the g_idle_add() like I have done above in sample C++ code

user2618142
  • 1,035
  • 2
  • 18
  • 35

1 Answers1

2

A pointer to a function is different than a pointer to a class method. You have to have an object of the class if you want to use a pointer to a method. You should wrap your method in a C function. For example:

extern "C"
{
    void myCFunction(void *p_SomeVariable)
    {
        // store your object where you want 
        static AnotherClass    mObjOfAnotherClass; 
        mObjOfAnotherClass.functionB(*p_SomeVariable);  
    }
}

And then:

g_idle_add ((GSourceFunc) myCFunction, p_SomeVariable);
nouney
  • 4,363
  • 19
  • 31