TF1 pointing to member function

Dear all,

I have a class with a TF1 as member. This TF1 should point to a member function of this class, something like

class Testclass { public: Testclass() { function = new TF1("spectrum",this,&Testclass::member,1,10,0); } TF1 * function; private: double member(double *x, double *p) { return x[0]; } };

This works fine as long as I just create one object of this class. Creating more than one object leads to a segfault (see attachment for a running example).

One solution I have found is to let TF1 point to an external wrapper class which itself holds a pointer to the particular “Testclass” object to which the TF1 belongs (and then calls pointer_to_testclass_object->member()). Moreover, the name of the TF1 apparently must be different for every instance of Testclass.

However, all this seems rather clumsy to me. What would be the correct way of doing something like this?

Thanks for your help,

Jens
testmacro.cxx (357 Bytes)

Hi,

if you don’t want to have different name for every istances of TF1 created, you can just add this line in the TClass constructor to remove the functions from the global list. This avoids that they are being deleted next time a new one with the same name is constructed

class Testclass { 
   public: 
Testclass() { 
         function = new TF1("spectrum",this,&Testclass::member,1,10,0); 
          gROOT->GetListOfFunctions()->Remove(function); 
      } 

...
};

Best Regards

Lorenzo