← Back to team overview

yade-dev team mailing list archive

Re: acces to sphere/wall information

 

> >> if ( !b ) continue;
> >> if (dynamic_cast<Sphere*>(b->shape.get()))
> >> {
> >> Sphere* s=YADE_CAST<Sphere*> ( b->shape.get() );
> >> const body_id_t& id = b->getId();
> >> cout << "is it a sphere?? id == " << id << endl;
> >>
> >> }
> >>     
> I think this will give segfaults in optimized builds, where YADE_CAST is 
> static_cast, right?
No, since static_cast is called inside condition where dynamic_cast
checked the type already. It is useless to use YADE_CAST after
dynamic_cast, though (two dynamic_casts in debug mode).

> If I remember correctly, dynamic_casts are very slow, so I would go for 
> the second option.
I never measured it. It cannot be in principle slower than virtual
function dispatch (since that also has to determine exact instance type,
just like dynamic_cast), and those are used allover yade (every functor
call is a virtual function, getClassIndex is virtual as well etc.

> I saw something like a "getStaticIndex" in one of the previous message 
> on this topic, could it be better?

You don't have to create the temporary, so instead of something like

  int sphereIdx=Sphere().getClassIndex(); // Sphere() creates a temporary, which is deleted immediately

you can have

  int sphereIds=Sphere::getClassIndexStatic();

Cheers, Vaclav




Follow ups

References