C ++ calls a class function inside another function

I am trying to call an external function (which takes functions as inputs) inside a class, providing the member functions of the class as parameters. My code looks like this:

using namespace std;
// This is my external function
double integrate (double a, double b, int steps, double (*func)(double)){
    // code for integration using function pointer
};

Class A
{
    protected:
        vector<double> to_data, from_data;
        int data_size;

    public:
        A (); // constructor

        double calculate_parameter(double t){
            // some class member function that makes use of other class members
        };

        double get_result(double x){
            double sum = 0;
        for (int i=0;i<data_size;i++){
           // this is basically what causes the error
            sum = sum + integrate(to_data[i],from_data[i],1,calculate_parameter);
        }
 };

}

      

However, this shows me an error that the calculate_parameter function cannot be converted. I figured out how to solve this problem, it is to change the outer function so that it also takes an object of the class. Is there a way to do this without instantiating a new class object? Thanks in advance!

+3


source to share


2 answers


A more general approach is to dump these function pointers from the 1970s and use technology in this millennium:

#include <functional>

double integrate (double a, double b, int steps, std::function<double(double)> f)
{
    // ...
}

class A
{
    double calculate_parameter(double t);

    double get_result(double x)
    {
        using std::placeholders::_1;

        double sum = 0;
        for (int i = 0; i < data_size; i++) {
            sum = sum + integrate(
                to_data[i],
                from_data[i],
                1,
                std::bind(&A::calculate_parameter, this, _1)
            );
        }

        return sum;
    }
};

      

This provides a simple and straightforward way to bind to almost any function and bake the this

member function pointer to the functor itself, without requiring any magic in the final call site (ie, inside integrate

, which literally just has to call f

!).




An even more "modern" way to do this is to wrap the call calculate_parameter

in lambda:

template <typename Callable>
double integrate (double a, double b, int steps, Callable f)
{
    // ...
}

class A
{
    double calculate_parameter(double t);

    double get_result(double x)
    {
        double sum = 0;
        for (int i = 0; i < data_size; i++) {
            sum = sum + integrate(
                to_data[i],
                from_data[i],
                1,
                [this](double x) { return this->calculate_parameter(x); }
            );
        }

        return sum;
    }
};

      

Note that I replaced std::function<double(double)>

with the computed template argument Callable

; you don't have to do this, but that doesn't mean you shouldn't convert lambda-ts std::function

when you don't need to. (Personally, I really enjoy being able to assert what it f

will accept double

and return double

without relying on its use to test it.)

+8


source


This calculate_parameter function must be static. More About Functions Fuction Objects



-1


source







All Articles