Global const variables

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 21 05:30:30 PDT 2014


On Tuesday, 21 October 2014 at 12:08:35 UTC, Solomon E wrote:
> On Tuesday, 21 October 2014 at 08:48:09 UTC, safety0ff wrote:
>> const int[] a;
>> int[] b;
>>
>> static this()
>> {
>>    b = [1];
>>    a = b;
>> }
>
> `a` isn't a reference to `b`. `a` is assigned by value and has 
> its own storage.

`a` is indeed a copy of `b`. But `b` is a pointer+length, and
only those are copied. The array data is not copied. `a` and `b`
refer to the same data afterwards.

[...]
> const int[] a;
> int[] b;
>
> static this()
>    {
[...]
>         a = b;
>     }
>
[...]
>
> void main()
> {
[...]
>     b = [8,7];

Here, making `b` point somewhere else (to [8, 7]). If instead you
change b's elements, you'll see that `a` and `b` refer to the
same data:

b[] = 8; /* Will also change `a`'s data. */


More information about the Digitalmars-d-learn mailing list