Find out most derived class in base class

Jonathan M Davis jmdavisProg at
Fri Nov 19 13:58:20 PST 2010

On Friday, November 19, 2010 13:37:45 div0 wrote:
> On 19/11/2010 21:22, Jonathan M Davis wrote:
> 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
> So just ignore the bit about dynamic cast completely then and repeat
> your first incorrect assertion.
> Yes D and C++ do provide a limited amout of runtime type information.
> It's called RTTI surprisingly enough. *cough*
> Dynamic cast, *explicitly* asks at *runtime* if some base class can be
> converted to a *more derived* class.
> You *can not* do that unless base classes know about the classes which
> *inherit from* them.

Yes. I know that. What I'm saying that you can't do is ask whether the type of 
an object has any derived types, let alone get a list of them. There's enough 
information there to get a list of the classes that a particular object has as 
its exact type and base classes, so you could (assuming that D gave you access 
to that information) figure out whether a particular base class has any derived 
classes (since you can see one of them), but there's no information there on 
whatever other classes might be derived from that class, and if it's the exact 
type that you're looking at, you don't have _any_ information on its derived  
classes. So,

class D : C //(with C being derived from B, and B being derived from A)

means that a D object has enough information to know about D, C, B, and A. but 
it wouldn't know about other classes such as

class E : D

class Q : C

The type info has information on that type and its base classes, not all of the 
classes in its class hierarchy. That's enough to properly cast the object and 
whatnot, but not enough to enquire about what derived classes may or may not 

- Jonathan M Davis

More information about the Digitalmars-d-learn mailing list