Get which derived class an object is if it's stored in an array of its base class

Morimur55 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 15 06:45:40 PDT 2017


On Saturday, 15 July 2017 at 13:12:49 UTC, Adam D. Ruppe wrote:
> On Saturday, 15 July 2017 at 13:02:52 UTC, Morimur55 wrote:
>> is there a way to check without attempting to cast to every 
>> derived type?
>
> The `typeid(obj)` will give the type... but why do you need it? 
> The classinfo returned by that doesn't give a lot of info.
>
> Casting is how you actually get the object, though you might be 
> better off putting the necessary methods in the base class.

Well I want to cast to the derived type so I can use a method 
that's defined in the base class, but is overridden in several of 
the derived types... and calling it without a cast seems to give 
me the base type functionality, but I'd like the derived type 
functionality when it's defined.

I have a lot of derived types, some of which may be added in 
future by someone else... was hoping something like 
`cast(typeid(obj))` was going to work... but my compiler doesn't 
like that :(


More information about the Digitalmars-d-learn mailing list