Extending existing classes

This MIGHT be more applicable to the CINT forum.

I have gotten into the habit lately of writing subroutines that I declare as derived from an existing ROOT class. This has worked reasonably well until I recently tried to compile the code with ACLiC. Basically, I want to be able to write new subroutines that act on objects that have been defined in ROOT. I would rather not write an entirely new class when the original is perfectly serviceable.

An example of this code is below:

#include <TLine.h>
#include

//Float_t GetSlope(TLine *line){
Float_t TLine::GetSlope(){

Float_t x0,y0,x1,y1;

x0 = this->GetX1();
x1 = this->GetX2();
y0 = this->GetY1();
y1 = this->GetY2();

/*
x0 = line->GetX1();
x1 = line->GetX2();
y0 = line->GetY1();
y1 = line->GetY2();
*/

Float_t m;
if(x0<x1){
m = (y1-y0)/(x1-x0);
}else{
m = (y0-y1)/(x0-x1);
}
return m;
}

void test(){

TLine *line = new TLine(2.,3.,7.,9.);

Float_t m = line->GetSlope();
// Float_t m = GetSlope(line);

cout<<"m = "<< m<<endl;

}

This code will run perfectly as a named script in CINT (.x test.C), but ACLiC complains that “GetSlope” is not a member function of TLine (.L text.C++).

I can make the program compile simply by using the version that is commented out, but I would have to change a good deal of code that I’ve written the other way, and I’d frankly rather not. Plus I think the way I’ve done it is just more aesthetically pleasing!

Any advice?

Thanks for your help!

Mark

PS. While THIS code seems to work fine when repeated in the same ROOT session, I have more complex programs that will work correctly ONCE, but crash unless I restart ROOT. When I change over to passing the objects as arguments (as above) this bizarre behavior stops.

Hi,

This code is not really legal C++ (i.e. you are not be allowed to simply ‘replace’ a member function). You should inherit from TLine, put the function in the new class and compile the resulting code (to get the virtual table correct)

Cheers,
Philippe.

So I have to create a new class that inherits from TLine?

Something like:

class TMyLine : public TLine{

public:
Float_t GetSlope();

ClassDef(TMyLine,1);
}


Mark

yes.

Philippe