On Sun, 13 Feb 2011 19:38:23 -0000, Kevin Bealer <kevindangerbea...@removedanger.gmail.com> wrote:

I don't know if you can find all of them easily but you can find the instantiated
ones by adding a line to the Foo constructor as shown here.

Two limits:

1. This doesn't report Bar itself since a Bar object is never created; however in a sense a 'Bar' object was created when Baz and Qux are created. Since you know how to get the parent of a type you should be able to fix this if desired.

2. As mentioned you can't get the non-instantiated classes this way -- it only detects classes as 'new' is called on them. By the way this wouldn't work in C++ because in C++ object identity changes as the successive constructors are called
-- it would just report Foo.

3. Of course you could add a pure virtual function to the class...

testrtti.d:

import std.stdio;

int[string] fooTypes;

class Foo {
    this() { fooTypes[this.classinfo.name] = 1; }
};

class Bar : Foo {
};

class Baz : Bar {
};

class Qux : Baz {
};

int main()
{
    Foo a = new Foo;
    Foo b = new Qux;
    Bar f = new Baz;

    foreach(key, value; fooTypes) {
        writefln("foo subtype: %s", key);
    }

    return 0;
}

foo subtype: testrtti.Foo
foo subtype: testrtti.Baz
foo subtype: testrtti.Qux

Kevin

Thanks for reply, I'm going to test it out now.
I guess there's no chance of getting the derived classes during the compile time, so I'll have to use this runtime approach.

Reply via email to