Merhaba Ali Hocam,
Bir süredir sıradan dizi imkanlarını kullanarak matrisler üzerinde işlem yapıyorum. Ama .dup istediğim gibi çalışmıyor. En sonunda sadeleştirilmiş örnek hazırlayıp emin oldum. Tek boyutluya çevirdiğinizde (xy'den x'e geçtim) lokaldeki dizi, yapı içinden yapılan değişiklikten etkilenmiyor:
1 2
^-- is copied the contents of the array into it...
100 200
^-- content of the array has changed.
Local array: [1, 2]
Acaba nerede hata yapıyorum? Sorunu şu kodda, altındaki çıktısı ile birlikte görebilirsiniz:
import std.format, std.stdio;
struct Bar {
int[] x;
int[][] xy;
this (int[] arr) {
x = arr.dup;
}
this (int[][] arr) {
xy = arr.dup;
}
void toString(void delegate(const(char)[]) str) const {
str.formattedWrite!"%(%(%3s %)\n%)"(xy);
str.formattedWrite!"%(%3s %)\n"(x);
}
}
void main() {
int[] x = [1, 2];
int[][] xy = [ [1, 3], [2, 4] ];
auto foo = Bar(xy);
foo.writeln(" ^-- is copied the contents of the array into it...");
foo.xy[0][0] = 100;
foo.xy[1][0] = 200;
foo.writeln(" ^-- content of the array has changed.");
"\nLocal array: ".writeln(xy);
// But the local has changed too!
}/* ÇIKTISI:
1 3
2 4
^-- is copied the contents of the array into it...
100 3
200 4
^-- content of the array has changed.
Local array: [[100, 3], [200, 4]]
*/
Bu konuda İngilizce forumlarda paylaşılmaya değer bir konu mudur? Yoksa çift boyutlu dizilerde .dup farklı bir şekilde mi kullanmak gerekiyor?
Teşekkürler...