TMethodCall

Hi,

Trying to use the TMethodCall, I realize I do not know how to pass parameters (or even whether what I’d like to do is feasible).

Knowing a classname and the prototype of one of its constructors, I’d like to use TMethodCall to create an object.

For instance : class A : public TObject { public: A(const SomeOtherClass& x, Int_t y, Int_t z); } I do :[code]TClass* c = TClass::GetClass(“A”);

TMethodCall call;

call.InitWithPrototype(c,className,“SomeOtherClass&,Int_t,Int_t”);

if ( call.IsValid() )
{
SomeOtherClass b;

const char* params = ???

Long_t rl;

call.Execute(params,&rl);

return reinterpret_cast<A*>(rl); ???
}
[/code]
How should I prepare the params string ? And is the return value a pointer to the class I expect or something else ??

Thanks,

Hi,

You can not call a constructor directly (neither in C++ nor via CINT), you always need to call it via the operator new.

You can default construct an object via: TClass* c = TClass::GetClass("A"); A *a = reinterpret_cast<A*>( c->New() );You can construct an object via any constructor using gROOT->ProcessLineFast: TString params; params.Form("*((SomeOtherClass*)0x%p),%d,%d",&b,1,3); A *a =reinterpret_cast<A*>( gROOT->ProcessLineFast(Form("new A(*((SomeOtherClass*)0x%p),%d,%d",&b,1,3)));and you can call any other function with TMethodCall.
The params must be the string you would have written if you called the function in compiled code minus the function name and the paranthesis. The result (if a pointer or reference) can be indeed reinterpret_cast as you did: SomeOtherClass b; TString params; params.Form("*((SomeOtherClass*)0x%p),%d,%d",&b,1,3); call.Execute(a, params,rl); a = reinterpret_cast<A*>(rl);

See the full example in the attachement.

Cheers,
Philippe.
mc.C (1021 Bytes)