confusion about structs

Christian Köstlin christian.koestlin at gmail.com
Sun Sep 25 14:40:36 PDT 2011


I have this small program, which does some data handling with structs:

struct App {
   private int[] fData;
   this(size_t initialSize) {
     fData = new int[initialSize];
   }
   void put(int i) {
     fData[0] = i;
   }
   int[] get() {
     return fData;
   }
}

struct Builder {
   int i;
   App app = App(2);
   this(int i_) {
     i = i_;
//    app = App(2);
   }
   void put(int v) {
     app.put(v);
   }
   int[] get() {
     return app.get();
   }
}

int[] get(int i) {
   auto b = Builder(i);
   b.put(i);
   return b.get();
}


unittest {

   auto h1 = get(1);
   auto h2 = get(2);
   assert(h1[0] == 1);
   assert(h2[0] == 2);
}

int main(string[] args) {
   return 0;
}

compiling this with dmd -unittest shows, that the tests are failing.
if you comment in the line //    app = App(2); then the tests work as 
expected.
Could you please explain what struct Builder { App app = App(2); } does 
exactly compared to doing thins in the structs constructor?

thanks in advance

christian



More information about the Digitalmars-d-learn mailing list