Problem with insertBack

John Nixon via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 3 07:34:31 PDT 2016


I recently came across another problem with my program in D, 
found a minimal program showing it, and experimented a little 
with it as follows:

import std.stdio;
import std.container;
struct CS{
   char[] t;
   CS dup()const{
     CS cs;
     cs.t = this.t.dup;
     return cs;}
};
void main(){
   Array!CS cs_array = make!(Array!CS)();
   CS cs;
   cs.t = "bb".dup;
   cs_array.insertBack(cs);
   write("cs_array = ");foreach(i2;cs_array)write(i2);writeln("");
   cs.t[0] = ("a".dup)[0];//this changes cs_array!
   write("cs_array = ");foreach(i2;cs_array)write(i2);writeln("");
   cs.t = "c".dup;//but this does not
   write("cs_array = ");foreach(i2;cs_array)write(i2);writeln("");
   return;}

The first assignment to cs.t[0] changes cs_array as indicated, 
but the second to cs.t does not. Also if these two assignments 
are reversed, neither affects cs_array. What could be the 
explanation of this?

Kind regards

John Nixon



More information about the Digitalmars-d-learn mailing list