About RTTI

Dear ROOTers,
is possible with the ROOT RTTI to have an istance of a class starting from the name of the class in a string?
I mean: I don’t know a priori the class I need. At runtime I read this info from a file, so I have the name of the class in a string, and i want to create an istance of this class.

I know that you can do it in Java in a rather simple way, but in Java all this stuff is included in the language itself (all classes implicitely derives from the common base class “object” and so on…)
In fact I don’t even know if it is possible in c++ …

Thank you

Matteo

Hi Matteo,

const char* myClassName = "TMyClass"; TClass* cl = gROOT->GetClass(myClassName); TMyCommonBaseClass* obj = dynamic_cast<TMyCommonBaseClass*>(cl->New());
works, if your class has a default constructor.

Cheers, Axel.

It’s possible to do it with no default constructor in a class:

MyClass *my = (MyClass*)gROOT->ProcessLineFast("new MyClass(param1, param2 )")

ROOT plug-in mechanism is based on it:
root.cern.ch/root/htmldoc/TPluginManager.html