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 11:44:07 PDT 2016


On Thursday, 24 March 2016 at 17:24:38 UTC, data pulverizer wrote:
> 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?

Sorry. Please disregard. I'll drive home and ask this question 
properly!


More information about the Digitalmars-d-learn mailing list