Dynamic_cast in CINT

Maybe I have not so much clear the situation but…
doing something like this

TF2 *fun = new TF2(.. something ..)
TH2 *histo = new TH2D(.. something ..)
TH2 *new_his = dynamic_cast<TH2D*>(fun)

I should obtain a null pointer in new_his, isn’t it?

Hi,

Current dynamic_cast is a noop in CINT.
Simply doing:TH2 *new_his = fun;is the best approximantion.
(or you can use TH2 *new_his = TH2D::Class()->DynamicCast(TF2::Class(),fun))
Cheers,
Philippe

[quote=“pcanal”]Hi,

Current dynamic_cast is a noop in CINT.
Simply doing:TH2 *new_his = fun;is the best approximantion.
(or you can use TH2 *new_his = TH2D::Class()->DynamicCast(TF2::Class(),fun))
Cheers,
Philippe[/quote]

Tnx for your reply.
So, if I’ve understood correctly with your first snippet it’s up to me to know
if I can do that while in the second one I have no this responsability but
at least I have to know the kind of object to cast; am I wrong?

Tnx in advance

Germano

WithTH2 *new_his = fun;
if fun is a TH2, this will work. If fun is not a TH2, it will issue a CINT syntax error and fail to run.
WithTH2 *new_his = TH2D::Class()->DynamicCast(TF2::Class(),fun)you get the same behavior as with a dynamic_cast.

Cheers,
Philippe.

Sorry Philippe if I was not clear before and
I apologize for that.

My snippet was only a coarse approximation of a
situation that happened to me.
Obviously it was not my purpose to cast a TF2 object
to a TH2 one; but this is what happened (for a mistake).

When I say that your second snippet

does not reproduce exactly tha dynamic_cast behaviour is because
I have to tell to DynamicCast( ) both the object (fun) and its type (TF2::Class()).
This wouldn’t have helped me to avoid mistakes because I’d have typed

TH2 *new_his = TH2D::Class()->DynamicCast(TH2::Class(),fun)

I hope that this discussion is not understood like a stupid nitpicking: my only
aim is to understand what can I do with CINT and what I cannot.
Maybe there are some things where I’m wrong in C++ itself rather than CINT
(and it is much likely) and this can be a good chance to clarify them.

Tnx for your patience

Cheers, Germano

Hi Germano,

what you’re trying to do is perfectly legal C++, only Cint can’t handle it (as Philippe pointed out, dynamic_cast doesn’t do anything in Cint). Still, Cint should complain when you try to assign fun to the TH2 pointer. Simply compile your code with Aclic, and you get the result you expect, as dynamic_cast is properly implemented by your compiler.

Axel.

Tnx for your reply Axel,

I have not used ACLiC yet (but AFAIK it is a good shortcut to avoid writing Makefile and some other stuff that I already did :frowning: );
by the way, in this case I think to have already done something similar for I compiled my class with rootcint support: so I entered a CINT environment
with my class compiled in and all members present into the CINT dictionary.
I tried it inside CINT, and there it’s where I get this trouble.

But ok, now I have understood the point and I know what I cannot do and
what I can (e.g. using DinamicCast()).

Tnx to both of you

Cheers, Germano