RE: TClonesArray typecasting

From: Philippe Canal <>
Date: Wed, 15 Nov 2006 11:21:29 -0600

> How come it doesn't work?

Simply because it is not (yet) implemented. We shall address it next year once the current major overhaul of CINT is done.


-----Original Message-----
From: Christian Holm Christensen [] Sent: Tuesday, November 14, 2006 11:57 PM To: Philippe Canal
Cc: 'Rene Brun'; 'George Locke'; 'roottalk' Subject: RE: [ROOT] TClonesArray typecasting

Hi Phillipe,

On Tue, 2006-11-14 at 22:24 -0600, Philippe Canal wrote:
> Hi,
> > When reading from a file, it's best to use dynamic_cast, as you may not
> > know the type of the object returned. Suppose there's an object in the
> > file named `o' and it's of class TH1F. If you do
> > TH2* h = dynamic_cast<TH2*>(file->Get("o"))
> > You will get a null pointer back, since `o' is not of type TH2 or
> > any of it's decedents.
> However this currently does not quite work in interpreted code
> because CINT currently degrade all the casts down to the equivalent
> of C style casts.

How come it doesn't work? It should be fairly easy to implement a the function `dynamic_cast<T>' to return a null pointer or through a bad_cast when the wrong conversion is requested.

Anyways, my point was mostly that we shouldn't promote old-style casts and instead ask people to use the C++ cast operators.

Finally, the dynamic_cast has more uses than just reading from a file. You could have a collection (TCollection, std::vector, std::list, etc.) in which you have objects of many different types, and it could make sense to check the type at runtime. I know in ROOT you can do use TObject::Class, but if you class does not derive from TObject or do not use the ClassDef macro then this member function is not available. And there _are_ reason why you would not derive from TObject or use the ClassDef macro.

> TFile and TDirectory offer the following syntax:
> TH2* h; file->GetObject("o",h);

Which is very nice. However, it's not implemented for collections, as far as I know. Incidently, why isn't TDirectory a TCollection?  


 ___  |  Christian Holm Christensen 
  |_| |  -------------------------------------------------------------
    | |  Address: Sankt Hansgade 23, 1. th.  Phone:  (+45) 35 35 96 91
     _|           DK-2200 Copenhagen N       Cell:   (+45) 24 61 85 91
    _|            Denmark                    Office: (+45) 353  25 404
 ____|   Email:               Web:
 | |
Received on Wed Nov 15 2006 - 18:22:33 MET

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:32:02 MET