Array of objects and their inheritance

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 14 12:56:02 PDT 2015


On Thursday, 14 May 2015 at 19:00:16 UTC, tired_eyes wrote:
> First, I don't understand why we see array[2] as 'Child'. While 
> it is a 'Child', shouldn't it be shown as a 'Parent' due to we 
> explicitly create an array of 'Parents'?

It is getting the name through a virtual interface (a hidden one 
that has typeinfo).

class Base {
    string getName() { return "Base"; }
}

class Derived : Base {
    override string getName() { return "Derived"; }
}

Base b = new Derived();
b.getName() == "Derived"; // because the virtual function can 
still be called through an interface

> Well, if it's still a 'Child', why we can't access it's fields?

It is a Child object, but you are talking to it through the 
Parent interface, so only functions+members available on Parent 
can be accessed without casting it.

> And what is the proper way of storing a collection of inherited 
> objects without losing access to their fields and methods?

Best you can do is say

if(child = cast(Child) parentArray[0]) {
    // it is a child, now use child to access that
}


Though often a better way is to add an interface method that does 
it in the parent and is overridden in the child, just like with 
the getName above.


More information about the Digitalmars-d-learn mailing list