Global const variables

Solomon E via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 21 06:43:29 PDT 2014


On Tuesday, 21 October 2014 at 12:30:30 UTC, anonymous wrote:
> 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. */

You're right. Thank you, anonymous stranger.

Sorry about that, safety0ff. It looks like you were right and I 
was wrong.

`b[0] = 8;` or `b[] = 8;` changes a. Printing the values for &a 
and &b shows they're different pointers, but (a is b) returns 
true. So I still have more to learn about how it does that.


More information about the Digitalmars-d-learn mailing list