Is there a keyword to access the base class

Ali Çehreli acehreli at yahoo.com
Wed Jun 19 16:30:31 PDT 2013


On 06/19/2013 04:10 PM, Stephen Jones wrote:
>> Hm... would be a nice idiom to implement generically in D. Like a type
>> switch.
>>
>> -Steve
>
> That is what I would prefer, but I tried:
>
> writeln(to!(typeof(bars[1]))(bars[1]).val);
>
> to see if I could access the "DERIVED" (thanks) class type but even
> though bars[1] is initialized as a new Foos its type is still marked as
> Bar. So the question is, how do you find the derived class type when
> presented with only the super class?

typeid gives you a TypeInfo class:

   http://dlang.org/expression.html#TypeidExpression

   http://dlang.org/phobos/object.html#.TypeInfo

import std.stdio;

class Base
{}

class Derived1 : Base
{}

class Derived2 : Base
{}

void foo(Base b)
{
     writeln(typeid(b));
}

void main()
{
     foo(new Derived1());
     foo(new Derived2());
}

Prints:

deneme.Derived1
deneme.Derived2

There is also TypeInfo_Class there which may be useful.

Ali



More information about the Digitalmars-d-learn mailing list