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

**URL:** <https://root-forum.cern.ch/t/how-to-inherit-some-parameters-from-a-main-class-to-a-function-object/44063>\
**Category:** Newbie\
**Created:** [March 19, 2021, 3:44am UTC](https://root-forum.cern.ch/t/how-to-inherit-some-parameters-from-a-main-class-to-a-function-object/44063 "2021-03-19T03:44:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DBoni](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dboni/32/24534_2.png) [@DBoni](https://root-forum.cern.ch/u/DBoni)\
**Post date:** [March 19, 2021, 3:44am UTC](https://root-forum.cern.ch/t/how-to-inherit-some-parameters-from-a-main-class-to-a-function-object/44063/1 "2021-03-19T03:44:04Z")

</div>

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:

```auto
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.

```auto
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:

```auto
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++

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 19, 2021, 8:01am UTC](https://root-forum.cern.ch/t/how-to-inherit-some-parameters-from-a-main-class-to-a-function-object/44063/2 "2021-03-19T08:01:40Z")

</div>

I think @moneta can help you.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 19, 2021, 6:30pm UTC](https://root-forum.cern.ch/t/how-to-inherit-some-parameters-from-a-main-class-to-a-function-object/44063/3 "2021-03-19T18:30:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DBoni](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dboni/32/24534_2.png) [@DBoni](https://root-forum.cern.ch/u/DBoni)\
**Post date:** [March 19, 2021, 7:35pm UTC](https://root-forum.cern.ch/t/how-to-inherit-some-parameters-from-a-main-class-to-a-function-object/44063/4 "2021-03-19T19:35:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 22, 2021, 9:08am UTC](https://root-forum.cern.ch/t/how-to-inherit-some-parameters-from-a-main-class-to-a-function-object/44063/5 "2021-03-22T09:08:20Z")

</div>

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:

```auto
// 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
