.classinfo by Class name

Justin Whear justin at economicmodeling.com
Thu Dec 1 14:31:46 PST 2011


.classinfo is for getting information about an object at __runtime__.

For the example given here, you don't even need templates:

void mustBeFruit(Fruit fruit)
{
    writeln(fruit.classinfo.name);
}

Since Apple, Banana, and Orange inherit from Fruit, they __are__ Fruit and 
can be passed to mustBeFruit. Getting the classinfo will then allow you to 
determine, at runtime, which subclass they are.

Justin


Adam wrote:

> Nevermind - sorry for the clutter.
> 
> For those who are apparently as dense as I am, this can be roughly
> accomplished via Template specialization:
> 
> class Fruit {}
> 
> class Apple : Fruit {}
> 
> class Celery {}
> 
> void mustBeFruit(T : Fruit)() {
>     writeln(T.classinfo.name);
> }
> 
> void main() {
>     mustBeFruit!(Apple)(); // Ok
>     mustBeFruit!(Celery)(); // Does not compile
> }



More information about the Digitalmars-d-learn mailing list