Just-In-Time Compilation with Classes

I’m trying to use Jitting with Classes but am having issues. I’ve been trying to run the following test code (just to see if I can get it to work):

cpp_code = """
class TestClass
{
    private:
        int num;
        
    public:
        TestClass(int num);
        int ReturnNum();
};

TestClass::TestClass(int num)
{
    this->num = num;
}

int TestClass::ReturnNum()
{
    return this->num;
}
"""

And then running:

ROOT.gInterpreter.ProcessLine(cpp_code)

mocm = ROOT.TestClass(17)
mocm.ReturnNum()

And the error message is:

input_line_53:17:12: error: qualified reference to ‘TestClass’ is a constructor name rather than a type in this context
TestClass::TestClass(int num)
^
input_line_53:17:26: error: expected ‘(’ for function-style cast or type construction
TestClass::TestClass(int num)

I’ve been able to get jitting to work for just straight up functions, but not for classes. What am I doing wrong?


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi @Diracula ,
I think ProcessLine tries to execute the code and trips up on the fact that the code is not executable. You want Declare in this case:

In [1]: import ROOT

In [2]: code = """
   ...: class TestClass
   ...: {
   ...:     private:
   ...:         int num;
   ...:
   ...:     public:
   ...:         TestClass(int num);
   ...:         int ReturnNum();
   ...: };
   ...:
   ...: TestClass::TestClass(int num)
   ...: {
   ...:     this->num = num;
   ...: }
   ...:
   ...: int TestClass::ReturnNum()
   ...: {
   ...:     return this->num;
   ...: }
   ...: """

In [3]: ROOT.gInterpreter.Declare(code)
Out[3]: True

In [4]: t = ROOT.TestClass(17)

In [5]: t.ReturnNum()
Out[5]: 17

Cheers,
Enrico

Sorry, I did that, but forgot to include it in the code. I’ve amended the code to reflect that.

The trick is using Declare instead of ProcessLine, see my example above (if you scroll down you can see the full working example).

Oh, sorry, I misunderstood your response (for some reason). That worked, thanks!