not expected pointers for struct members from foreach

deed none at none.none
Tue Oct 9 08:30:27 PDT 2012


import std.stdio;


struct Test
{
     static Test[]  objects;
     static Test*[] psObject;
     static int[]   ints;
     static int*[]  psInt;
	
     int  a;
     int	 b;
     int* pa;
	
     this(int a)
     {
         this.a = a;
         this.pa = &this.a;
         this.b = 2 * a;
		
         objects  ~= this;
         psObject ~= &this;
         ints     ~= b;
         psInt    ~= &this.b;
     }
}


void main()
{
     Test(20);               // /*
     Test(21);               //  * As expected.
     Test(22);               //  */
	
     foreach(int i; 0 .. 3)  // /*
     {                       //  * What happens here?
         Test(30 + i);       //  *
     }                       //  */
	
     Test.psObject.writeln;
         // Prints [7FFFA4E84530, 7FFFA4E84540, 7FFFA4E84550,
         //         7FFFA4E84570, 7FFFA4E84570, 7FFFA4E84570]
         // Why are the three last adresses the same?

     Test.ints.writeln;
         // Prints [40, 42, 44, 60, 62, 64]
         // As expected.

     Test.psInt.writeln;
         // Prints [7FFFA4E84534, 7FFFA4E84544, 7FFFA4E84554,
         //         7FFFA4E84574, 7FFFA4E84574, 7FFFA4E84574]
         // Again, why are the three last adresses the same?
}


More information about the Digitalmars-d-learn mailing list