Struct Inheritence

user001 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 19 13:58:24 PST 2016


Well struct don't have it, but i would like something like it but 
only for data, i don't need functions or anything like that just 
data.


     struct A
     {
         int valueA;
     }

     struct B
     {
         A a;
         int valueB;
     }

     struct C
     {
         B b;
         int valueC;
     }

     C c;

     c.b.a.valueA; // not elegant

     B* b = &c.b;

     b.a.valueA; // we can access A


     // alternative 
--------------------------------------------------

     struct A
     {
         int valueA;
     }

     struct B
     {
         int valueB;
     }

     struct C
     {
         A a;
         B b;
         int valueC;
     }

     C c;

     c.a.valueA; // a bit more elegant but still not very, 
c.valueA would be prefered

     B* b = &c.b;

     b.? // can't access A, unless we do some hack that assumes B 
always follows A in the definition


Is there any way to do inheritance with structs, is there a 
reason why we can't extend structures like in C++?


More information about the Digitalmars-d-learn mailing list