I'm having a great deal of problems trying to make a callback system. I want to pass a function to another class's function to receive data.
I want ExampleClass
to call SeperateThread::operate
, and I want SeperateThread::operate
to be able to call ExampleClass::updateNumber(int)
to return a value. I've been trying for hours with various function pointers etc but can't seem to get it to work.
SeperateThread
is another thread so it doesn't block the main thread that ExampleClass
is running in, but when SeperateThread
has done it's calculations, I need to return the value to ExampleClass
.
If that makes sense? Here's a run down of what I'm trying to do. In this example, I want SeperateThread::operate
to call ExampleClass::updatenumber(15);
...
class ExampleClass
{
public:
ExampleClass()
{
int numberToPass = 10;
// call SeperateThread::operate and pass value and updatenumber function as pointer
thread.operate(numberToPass, *updatenumber(int number));
}
~ExampleClass();
void updatenumber(int number)
{
// Do some stuff to the number passed to this function
}
private:
SeperateThread thread;
}
class SeperateThread
{
public:
SeperateThread();
~SeperateThread();
void operate(int number, &FunctionToCallBack)
{
// Do some calculations (result 5 for example purposes)
int result = numberToPass + 5;
// Call the callback function and pass result int
FunctionToCallBack(result);
}
}