mercredi 28 décembre 2016

Using lambda expression to pass a class function member as a parameter in C++11

The following minimalist codes are used to illustrate my question. Those codes do not compile. How can I use lambda expression or std::bind to pass a function member as a parameter? Many thanks for your help

 #include <iostream>

    using namespace std;
    class ABC{
    private:
        int x =3;
        int add2num(int a, int b){
            return a+b+x;
        }

        int worker(int &fun(int a, int b), int a, int b){
            return fun(a,b);
        }
    public:
        int doSomething(int a, int b){
            return worker(add2num, a, b);
        }
    };

    int main() {
        ABC test;
        cout << test.doSomething(3,5) << endl;
        return 0;
    }

Aucun commentaire:

Enregistrer un commentaire