Cast vs Virtual Method vs TypeId?
rikki cattermole via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jun 29 17:27:57 PDT 2016
On 30/06/2016 12:25 PM, Jonathan Marler wrote:
> I'd like to hear peoples thoughts on the various solutions for the
> following problem. Say you have some hierarchy of classes like:
>
> class GameObject {
> // ...
> }
> class Entity : GameObject {
> // ...
> }
> class Player : Entity {
> // ...
> }
> class Enemy : Entity {
> // ...
> }
> // ...
>
> Assume you have a function that accepts a GameObject but does something
> special if that GameObject happens to be an instance of the Player
> class. How would you go about determining this? (Note: assume you need
> to make the distinction at runtime, so you can't use a static if with an
> 'is' expression inside a template.)
void func(GameObject o) {
if (Player player = cast(Player)o) {
// something special
}
}
More information about the Digitalmars-d-learn
mailing list