is there any way to get a list of classes that inherit a class?
Kevin Bealer
kevindangerbealer at removedanger.gmail.com
Sun Feb 13 11:38:23 PST 2011
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
More information about the Digitalmars-d
mailing list