Find out most derived class in base class

Jonathan M Davis jmdavisProg at gmx.com
Fri Nov 19 13:22:54 PST 2010


On Friday 19 November 2010 13:12:40 div0 wrote:
> On 19/11/2010 21:05, Jonathan M Davis wrote:
> > On Friday 19 November 2010 12:56:38 Steven Schveighoffer wrote:
> > 
> > And how would you know that at runtime? All reflection in D at this point
> > is compile-time reflection, and that isn't going to help you any here
> > (for the very reasons that you list). I don't see how you could
> > determine which classes are derived from a particular class at runtime.
> > You could use typeof() to determine what the exact type of a reference
> > is, but that wouldn't help you determine what derived classes exist for
> > a particular type. What you'd really need is runtime reflection, which D
> > doesn't have.
> > 
> > - Jonathan M Davis
> 
> How would dynamic cast work if you can't find out the inheriting classes
> from a reference?
> 
> At runtime, the runtime type info for classes forms a DAG.
> 
> As D only allows single inheritance it should be trivial to find the
> most derived class, though the runtime doesn't currently offer a
> function for this.

Just because an object is able to know what its actual type is - or even its 
base classes - does not mean that you could ask it what other types exist which 
are derived from one of its base types or its exact type. Sure, D definitely 
_could_ provide the necessary type information at runtime (C# and Java do that 
sort of thing - which is why thy can have runtime reflection), but it doesn't. At 
best, you can get information on the types of a particular object from that 
object, not the types which exist in general.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list