I’m trying to use operator overloading in some library I’m compliling and linking into ROOT through rootcint.
In my code I have :
ICalculator* operator+(ICalculator &c1, ICalculator &c2);
and in a LinkDef.hpp file :
#pragma link C++ class ICalculator;
#pragma link C++ function operator+(ICalculator &, ICalculator &);
Unfortunatly PyROOT (or python ?) is not enable to deal with it.
[code]>>> cn = ICalculator()
d = cn+cn
Traceback (most recent call last):
File “”, line 1, in
TypeError: unsupported operand type(s) for +: ‘ICalculator’ and ‘ICalculator’
[/code]
whereas CINT works :
the easiest solution would be to add the operator to the class (if you have control over the class, that is). Otherwise, you can still emulate this by grabbing the global function and adding it on the python side. This would look something like:for f in gROOT.GetListOfGlobalFunctions():
if f.GetPrototype() == "ICalculator* operator+(ICalculator& c1,ICalculator& c2)":
ICalculator._add, ICalculator.__add__ = f, lambda x, y: ICalculator._add(x,y)
break
Please note the right amount and positions of spacing as well as argument names in the prototype: it’s a string comparison and has to match exactly as written in the header file.
Python does not support global operator overloads …