samedi 1 avril 2017

c++ pointer to member function using std::function

I know this has been asked multiple times, most of the answers are difficult for me to understand. Could you please help me in figuring out what am I doing wrong ?

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <functional>

class A{
    public:
        A(double i){
            _b = i;
        }
        double square(double i){

            return i*i*_b;
        }


    private:
    double _i;
    double _b;
};

double cube(double (*objfunc)(double), double x){
    return objfunc(x)*x;   
}

int main()
{
    double v = 2.0;
    A a(v);

    using std::placeholders::_1;
    std::function<double(double)> f_square = std::bind( &A::square, &a, _1 );
    double x = cube(f_square,3.0);
    std::cout << " x = " << x << std::endl;


}

Thank you as always for your advice.

Aucun commentaire:

Enregistrer un commentaire