How could I pass std::function as function pointer?

前端 未结 3 1406
春和景丽
春和景丽 2021-01-15 09:23

I am trying to write a class template and internally it use a C function (implementation of BFGS optimization, provided by the R environment) with

3条回答
  •  天涯浪人
    2021-01-15 09:36

    An alternative solution could be to have the optim class do its magic with two (possibly pure) virtual functions, and then inherit to define a new class Rosen which implements them. This could look like

    class optim {
        public:
            // ...
    
            virtual double fn(int n, double *par, void *ex) = 0;
            virtual void gr(int n, double *par, double *gr, void *ex) = 0;
    
            void minimize(arma::vec &dpar, void *ex) {
                vmmin(... , &fn, &gr, ...);
                // ...
            }
    };
    
    class Rosen : public optim {
        public:
            // ...
            double fn(int n, double *par, void *ex);
            void gr(int n, double *par, double *gr, void *ex);
    
        private:
            // ...
    };
    
    // main.cc    
    Rosen obj;
    obj.minimize(dpar, ex);
    

提交回复
热议问题