Initialize member functions through the constructor

I may have left the left margin with this question, but is it possible to define a member function through the constructor?

In my case, I am trying to write a class to perform a robust fit of a model (using RANSAC ). I want this to be generalizable to different types of models. For example, I could use this to determine a plane estimate for a set of three-dimensional points. Or perhaps I could define a transformation between two sets of points. In these two examples, different error functions and different fitting functions may be required. Instead of using a class, a static function call might look like

model = estimate(data, &fittingFunc, &errorFunc);

I am wondering if I can have a member instance for these modular functions?

Sort of

class Estimator
{
    private:
        // estimation params

        double errorFunc(std::vector<dtype>, double threshold); // Leave this unimplemented
        double fittingFunc(std::vector<dtype>, Parameters p); // Leave this unimplemented

    public:
        Estimator(void (*fittingFunc(std::vector<dtype>, Parameters), void (*errorFunc(std::vector<dtype>, double));

        dtype estimate(data); // Estimates model of type dtype. Gets implemented

};

Estimator::Estimator(void (*fittingFunc(std::vector<dtype>, Parameters), void (*errorFunc(std::vector<dtype>, double))
{
    fittingFunc = fittingFunc;
    errorFunc = errorFunc;
}

, , , . : -?

-, , ?

UPDATE: , MATLAB , ​​ , ++

0
2

-?

. -. public member:

class Estimator
{
public:
    double (*errorFunc)(std::vector<dtype>, double threshold);
    double (*fittingFunc)(std::vector<dtype>, Parameters p);

public:
    Estimator(void (*fittingFunc(std::vector<dtype>, Parameters), void (*errorFunc(std::vector<dtype>, double))
    : errorFunc(errorFunc)
    , fittingFunc(fittingFunc)
    { }

    dtype estimate(data);    
};

( ) private -, .


, , std::function<double(std::vector<dtype>, double)> std::function<double(std::vector<dtype>, Parameters)>, ( , lambdas, -, .)

+4

, . , . , std:: function, , -.

:

 #include <functional>
 class Estimator
 {
 private:
    // estimation params
    using error_func_type =   std::function<double(std::vector<dtype>,double)>;
    using fitting_func_type = std::function<double(std::vector<dtype>,Parameters p)>;
    fitting_func_type fittingFunc;
    error_func_type errorFunc;


public:
    Estimator(fitting_funct_type fit, error_funct_type err)
      :fittingFunc(fit),errorFunc(err){}

    dtype estimate(data); // Estimates model of type dtype. Gets implemented

 };
+1

Source: https://habr.com/ru/post/1669491/


All Articles