Overriding abstract class fields

slaid via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 1 04:09:18 PDT 2016


I have a snippet:

   import std.stdio;
   import std.algorithm;

   abstract class A {
       int height = 0;
   }

   class B : A {
   }

   class C : A {
       int height = 1;
   }

   void main() {
       A[][int] list;
       list[0] = new A[](0);
       list[0] ~= new B();
       list[0] ~= new C();
       list[0] ~= new B();
       writeln(list[0].map!(x=>x.height));
   }

The output of this is [0, 0, 0] when if height was overridden it 
should print [0, 1, 0]. This is a trivial case for simplicity of 
the question, but in my actual code, I am trying to use the 
height field for sorting the A[] list (or reducing with max to 
get the object with the max height), but since they're all 0, 
none of the sorting occurs.

How do I override this height field?

Thanks


More information about the Digitalmars-d-learn mailing list