Class with a pointer in its constructor


This may be a trivial issue for experienced python users, but I’m quite stuck with it right now.

I have a class in C++ which I’d like to use from PyROOT. The constructor of this class receives the pointer to an integer in its constructor, with something like this:

I can successfully generate a dictionary for this class using rootcint, I also manage to load this dictionary from PyROOT, after which the class itself seems to be known to ROOT, because I can try to instantiate an object of this type. However the instantiation fails with this message:

Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: none of the 2 overloaded methods succeeded. Full details: IsoMuonFeatureD3PDObject::IsoMuonFeatureD3PDObject(Int_t* master, const char* prefix = "trig_L2_isomuonfeature_") => could not convert argument 1 IsoMuonFeatureD3PDObject::IsoMuonFeatureD3PDObject(const D3PDReader::IsoMuonFeatureD3PDObject&) => could not convert argument 1

In this case I was trying to instantiate the object like this:

[code]>>> myint = 0

obj = ROOT.D3PDReader.IsoMuonFeatureD3PDObject( myint )[/code]

On the C++ side using a pointer like this is the most elegant solution. But if you tell me that I won’t be able to use this code from python, then I’ll have to re-consider how to implement the C++ class.



the int* is seen as an array of ints, and python array objects can be passed through it:

from array import array a = array('i', [0]) obj = ROOT.D3PDReader.IsoMuonFeatureD3PDObject(a)
Please note that the C++ class needs to either copy the values from the array, or the python side needs to keep it alive. Otherwise, refcounting will eat “a” and you’d have a dangling pointer on the C++ side. Thus, if the C++ code does not copy the value, do something like:

To make the lifetime of obj and a equal. Of course, you can wrap that up by modifying init on the python side, if this code is used often (and e.g. the size of the array is always known).