Cast vs Virtual Method vs TypeId?

Jonathan Marler via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 30 09:58:35 PDT 2016


On Thursday, 30 June 2016 at 00:27:57 UTC, rikki cattermole wrote:
> On 30/06/2016 12:25 PM, Jonathan Marler wrote:
>> 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
> 	}
> }

Thanks for the response.  Do you also have any information on how 
cast works under the hood?



More information about the Digitalmars-d-learn mailing list