www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Simple code sample of Nesting Structures. I'm I doing something

reply "WhatMeWorry" <kheaser gmail.com> writes:
// 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 =
Nov 22 2014
next sibling parent "Chris Nicholson-Sauls" <ibisbasenji gmail.com> writes:
On Saturday, 22 November 2014 at 20:57:07 UTC, WhatMeWorry wrote:
         auto bottom = NestedBottom(2, ['d','o','g']);
That 'auto' is the problem. You want 'this.bottom = ...' instead.
Nov 22 2014
prev sibling next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 22 Nov 2014 20:57:05 +0000
WhatMeWorry via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

          auto bottom =3D NestedBottom(2, ['d','o','g']);
ah, that good old thingy! there were some debates about locals that shadows fields and how that can introduce some hard-to-catch bugs. you were hit by exactly that: you creating local variable `bottom` instead of assigning value to member `bottom`. that nasty little compiler is playing tricks with us all.
Nov 22 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
ketmar:

 there were some debates about locals that
 shadows fields and how that can introduce some hard-to-catch 
 bugs.
I told ya. Bye, bearophile
Nov 22 2014
prev sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Sat, Nov 22, 2014 at 11:54:01PM +0200, ketmar via Digitalmars-d-learn wrote:
 On Sat, 22 Nov 2014 20:57:05 +0000
 WhatMeWorry via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
 wrote:
 
          auto bottom = NestedBottom(2, ['d','o','g']);
ah, that good old thingy! there were some debates about locals that shadows fields and how that can introduce some hard-to-catch bugs. you were hit by exactly that: you creating local variable `bottom` instead of assigning value to member `bottom`. that nasty little compiler is playing tricks with us all.
Yeah, this bit me before, and it was Not Fun. Generally, it's a very bad idea to name local variables after field members due to shadowing. I'd file a bug for it, but I'm not sure how likely it is to get fixed. But you could try. ;-) (Sometimes it works. :-P) T -- Leather is waterproof. Ever see a cow with an umbrella?
Nov 22 2014