Simple code sample of Nesting Structures. I'm I doing something illegal here?

WhatMeWorry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 22 12:57:05 PST 2014


// Two simple value type structures. one embedded in the other. 
I've stepped through the debugger and I see the embedded 
structure being set to 2, and dog.



import std.stdio;

struct NestedBottom
{
     int i;
     char[3] fixedArray;
     // this(){}  no-argument ctor can only be defined by the 
compiler
     this(int i, char[3] fixedArray)
     {
         this.i = i;
         this.fixedArray = fixedArray;
     }
     void print()
     {
         writeln("This is level ", i);
         writeln("animal = ", fixedArray);
     }
}

struct NestedTop
{
     int i;
     char[3] fixedArray;
     NestedBottom bottom;
     // this(){}  no-argument ctor can only be defined by the 
compiler
     this(int i, char[3] fixedArray)
     {
         this.i = i;
         this.fixedArray = fixedArray;
         auto bottom = NestedBottom(2, ['d','o','g']);
     }
     void print()
     {
         writeln("This is level ", i);
         writeln("animal = ", fixedArray);
         bottom.print();

         // added this in desperation. Still nothing.
         writeln("This is level ", bottom.i);
         writeln("animal = ", bottom.fixedArray);
     }
}


void main()
{
     auto top = NestedTop(1, ['c', 'a', 't']);
     top.print();
}



Output is the following:

This is level 1
animal = cat
This is level 0
animal =
This is level 0
animal =




More information about the Digitalmars-d-learn mailing list