RE: [ROOT] how to get correct class from root file ?

From: Axel Naumann (axel@fnal.gov)
Date: Fri Jan 25 2002 - 16:52:46 MET


Hi,

you always know that the objects derive from TObject (otherwise Get won't
work). And once you read the objects they'll have a vtbl set up, meaning
they know what "their" Test() is. So a simple

TClassA* BaseA;
BaseA=(TClassA*)File->Get("ClassA");
BaseA->Test(); (calls Test() of ClassA)
TBase* BaseB;
// just casting the pointer doesn't cast the object its pointing to:
BaseB=(TClassA*)File->Get("ClassB");
BaseB->Test(); (calls Test() of ClassB)

should work.

Cheers, Axel.

> -----Original Message-----
> From: owner-roottalk@pcroot.cern.ch
> [mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of joer00@bellsouth.net
> Sent: Friday, January 25, 2002 9:30 AM
> To: roottalk@pcroot.cern.ch
> Subject: [ROOT] how to get correct class from root file ?
>
>
> Hi,
>
> I have Class A with a virtual function Test() and Class B with
> virtual function Test().
>
> No I save A and B to a root file. I can get the classtype of each
> key with GetClassName, but how can I store EACH class in a
> pointer variable of Class A ?? I must do a type cast when I call
> File->Get() and this must be in a way DYNAMIC.
>
> An Example:
>
> I want:
>
> ClassA* Base;
> Base=(ClassA*)File->Get("ClassA");
> Base->Test(); (calls Test() of ClassA)
> Base=(ClassB*)File->Get("ClassB");
> Base->Test(); (calls Test() of ClassB)
>
> But I dont KNOW at compile time what contents File has so it
> should be something like:
>
> ClassA* Base;
> Base=(key->GetClassName()*)File->Get("ClassA");
>
> But this is of course no valid C++ code !
>
> What is the correct way ?
>
> Joe
>
>
>
>
>
>
>
>



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:40 MET