Struct array assignment behaviour using example from Programming in D, chapter 78

data pulverizer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 24 10:24:38 PDT 2016


I have been playing with the matrix example given at the end of 
chapter 78 of Ali Çehreli's fabulous book and am having problems 
with overloading the opAssign operator.

rows is a private int[][] in a Matrix struct.

I have added the following ...

Matrix opAssign(int[][] arr)
{
     this.rows = arr;
     // rows = arr // does not work either ...
     return this;
}

However this does not work (no error occurs, it just doesn't do 
anything) but this does work ...

Matrix opAssign(int[][] arr)
{
     foreach(i, row; rows){
         row[] = arr[i];
     }
     return this;
}

The second is not efficient since it has to loop to assign the 
array. Is there a more efficient way of overwriting the array?



More information about the Digitalmars-d-learn mailing list