I have a worker class, which takes a callback function pointer initialization in the constructor. I would like to instantiate the worker in my class, and provide a pointer to a member function as a callback.
The worker class:
class Worker
{
public:
typedef int (*callFunct)(int);
Worker(callFunct callback = nullptr);
~Worker();
private:
callFunct m_callback = nullptr;
};
Worker::Worker(callFunct callback) : m_callback(callback)
{
}
and here is my class, where I would like to have Worker
instance and a member callback function:
class myClass
{
public:
myClass();
~myClass() = default;
private:
int myCallback(int x);
Worker m_worker {&myClass::myCallback};
};
Unfortunately this does not compile:
error: could not convert '{&myClass::myCallback}' from '<brace-enclosed initializer list>' to 'Worker'
I am aware that when dealing with pointer to a member class I have to provide the instance object as well. But what is the correct way to do in this situation, when I need to have pointer to class member?