back to arays

Tom S h3r3tic at remove.mat.uni.torun.pl
Mon May 15 03:24:39 PDT 2006


Derek Parnell wrote:
> On Mon, 15 May 2006 10:05:22 +0300, Max Samuha wrote:
> 
>> I thought array references are similar to object references like in C#
>> (actually, thay are object references in C#) and that was my mistake:
>>
>> int[] a = new int[20];
>> int[] b = a;
>>
>> a.length = 40; // a is copied and b is not updated to point to a's
>> data;
>>
>> Does it mean that anytime i change an array, i have to manually update
>> all references to it or should i wrap my array in an Array class so
>> that all references to any instance of that array remain valid?
>>
>> If the question have been already discussed please refer me to the
>> right thread. Thanks
> 
> I assume for some valid reason you want this behaviour...
> 
>  int[] a = new int[20];
>  int[] b = a;
>  int[] c = a;
>  a[0] = 17;
>  writefln("%s %s", b[0], c[0]); // Displays 17 17

But... it will display '17 17'.


> The simplest way to do this is  ...
> 
>  int[] a = new int[20];
>  int[]* b = &a;
>  int[]* c = &a;
>  a[0] = 17;
>  writefln("%s %s", b[0], c[0]); // Displays 17 17

Nope, the earlier one :)


As I understand it, he'd like this code:

#    int[] a = new int[20];
#    int[] b = a;
#    b.length = 10;
#    writefln("%s %s", a.length, b.length);

to output '20 20'.


-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d-pu s+: a-->----- C+++$>++++ UL P+ L+ E--- W++ N++ o? K? w++ !O 
!M V? PS- PE- Y PGP t 5 X? R tv-- b DI- D+ G e>+++ h>++ !r !y
------END GEEK CODE BLOCK------

Tomasz Stachowiak  /+ a.k.a. h3r3tic +/



More information about the Digitalmars-d-learn mailing list