A question about TClass::New(..)

Dear you,

I have a question a bout TClass::New(…), this maybe a silly question, but useful for me.

I have a code like this,

const char* cname = "UserClassName";
//by useing the TClass::New(..) method, I can create an object of 'UserClassName'
void *obj = gROOT->GetClass(cname)->New(0);

the ‘obj’ is a ‘void*’ type, my question is how can I cast ‘obj’ to it’s true type ‘UserClassName’ [color=#FF0000] by using the value of ‘cname’[/color].
I mean I want to do something like:


if I do not cast ‘obj’ to it’s true type, I can not excute it’s member functions.

Any help or advice is appreciated!


This can not be done directly in compiled code, however you can go through the interpreter. One way is:gROOT->ProcessLine(TString::Format("((%s *)%p)->somemethod()",cname,obj);Alternatively you can go through the TClass and TMethodCall interface.


Thanks pcanal.
Your method is useful, it works well.

Best regards.