简体   繁体   中英

C++ function pointer to member function

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?

Pointers to non-member functions are not the same as pointers to (non-static) member functions!

The difference is that a (non-static) member function needs an object to be called on, which pointers to non-member function doesn't have.

You can use std::function and lambdas to create the callback:

class Worker
{
public:
    using callFunct = std::function<int(int)>;

    Worker(callFunct callback)
        : m_callback(callback)
    {
    }

private:
    callFunct m_callback;
};

class myClass
{
public:
    myClass()
        : m_worker([this](int x) { return myCallback(x); })
    {
    }

private:
    int myCallback(int x);
    Worker m_worker;
};

Or if the callback doesn't need to acces the myClass object, then make the function static :

class myClass
{
public:
    myClass();

private:
    static int myCallback(int x);
    Worker m_worker {&myClass::myCallback};
};

[As mentioned by others in comments to the question]

Or a plain lambda that does what myClass::myCallback would do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM