How to inherit some parameters from a main class to a function object

Dear all,

I am developing a c++ code using ROOT to fit a graph with a function. I created a TF1 from a C++ function object (functor) with parameters. Therefore, I coded a C++ class implementing this member function:

class  MyFunctionObject : public ClassA {
 public:
  // use constructor to customize your function object
  MyFunctionObject(double D, double E, double F) 
  {
    //ctor
    fD = D;
    fE = E;
    fF = F;
 
   }

   double operator() (double *x, double *p) {
      // function implementation using class data members
   }
};

However, I want the class MyFunctionObject to inherit some parameters set in the constructor of the ClassA.

ClassA::ClassA(double A, double B, double C)
{
    //ctor
    fA = A; //fA, fB and fC are protected
    fB = B;
    fC = C;
}

How should I do the get these parameters values? The function to fit the graph looks like this:

const double *ClassA::GetFitParameters(const double *EstimVals, float *X, float *Y)
{
const double *xs=new double[9];

//some code here


MyFunctionObject fobj(....);       // create the function object
TF1 * f = new TF1("f",fobj,xmin,xmax,npar);    // create TF1 class with n-parameters and range [xmin,xmax]

//some code do the fit

return xs;

}

Regards,
Daniel


ROOT Version: 6.22
Platform: Linux
Compiler: g++


I think @moneta can help you.

Hi,

The way you create the TF1 object seems correct to me. I do not understand your question, the parameters fA,fB and fC are protected so they are available to be used in MyFunctionObject::operator(). You need however to set their values either in the constructor of MyFunctionObject or by calling some setter functions.

Lorenzo

Hi Lorenzo,

That is the point. I can’t access fA, fB and fC from ClassA to be used by MyFunctionObject::operator(). I am getting the error:
error: no matching function for call to ‘ClassA::ClassA()’

What am I doing wrong?

Regards,
Daniel

As I have written before you need a way to pass those A,B,C values.
For this you need to call from the constructor of the derived class MyFunctionObject the constructor of class A defining A,B,C;
Implement as following the MyFunctionObject constructor:

// use constructor to customize your function object
  MyFunctionObject(double A, double B, double C, double D, double E, double F)  : 
     ClassA(A,B,C)
{
    //ctor
    fD = D;
    fE = E;
    fF = F;
 
   }

Lorenzo