← Back to team overview

yade-dev team mailing list archive

Re: static_cast in GSTS

 

> Ok. So is the derived class object also a base class object? Exactly
> the same one?

You can always cast derived object to an instance of its base class,
that is the interest of inheritance, actually one of the most basic
principles of object-oriented programming.

Think of casting as changing point of view; a doctor can see his friend
as his friend, or as a concrete patient, or as an instance of generic
mankind species, depending on what he needs.

That happens when you cast. static_cast<FrictPhys*>(..) says that now
you want to see that object as a pointer to FrictPhys (heaven with you
if the cast is invalid -- dynamic_cast will give you NULL which you can
check, static_cast will skip that check and give you a valid but
nonsense pointer, which will most likely lead to crash).

The pointer is always exactly the same. You can try pointer comparison
on some data member:

FrictPhys* fp=static_cast<FrictPhys*>(i->interactionPhysics.get());
NormShearPhys* nsp=static_cast<NormShearPhys*>(i->interactionPhysics.get())
assert(&(fp->shearForce)==&(nsp->shearForce));

(When you access shearForce, internally you access the NormShearPhys
where it is declared; it is the compiler's job to resolve that).

HTH, Vaclav






Follow ups

References