TClass::New() with argument

I have a TClass * for a class and I want an instance of this class. In previous versions, this was done using TClass::New() with a default constructor, and it was fine.

However, now (for supported classes) I want to use an argument in the constructor (which for this purpose happens to be a single string), generated at run-time.

Is there any way to do this? I initially looked at trying to defining my own StringIOCtor class and using:

With this, however, I don’t see how I actually construct an instance of the StringIOCtor class that should be used - is the type just being used as a flag to indicate a different constructor should be used, without instantiating the object?

Hi,

I am not sure why you are trying to achieve this, but wouldn’t a solution based on the interpreter be viable for you, e.g.:

auto h = (TH1F*) gInterpreter->ProcessLine("new TH1F(\"myHist\",\"title\",64,-4,4);");

Cheers,
D

That looks like it would work. It is fairly ugly, but given what I’m trying to do is fairly ugly anyway, I shouldn’t really complain.

Thanks a lot.

Hi,

I guess “ugly” is a matter of taste, but glad the suggestion works for you :slight_smile: De facto, the interpreter allows you to have a simple and universal plugin factory.
Another solution could consist in creating a factory yourself, e.g. implementing factory methods in your libraries returning fresh class instances.

Cheers,
Danilo