array as parameter

Paul via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 7 14:33:22 PDT 2014


On Saturday, 7 June 2014 at 21:17:41 UTC, monarch_dodra wrote:
> On Saturday, 7 June 2014 at 20:56:14 UTC, Paul wrote:
>> Dynamic array is really reference. Right? But why modification 
>> of parameter in this case does not work:
>>
>> void some_func(string[] s) {
>> s ~= "xxx"; s ~= "yyy";
>> }
>>
>> but this works:
>>
>> void some_fun(ref string[] s) {
>> s ~= "xxx"; s ~= "yyy";
>> }
>>
>> In the 1st case s is reference too, is not it?
>
> It's a value type that holds a reference to data. If you modify 
> the *actual* slice, rather than the referenced items, then you 
> need to pass by ref.

Seems that string[] is not real reference as in C++. As I 
understand there is only one array of strings. And when I try to 
modify it in function with ref or whithout - array is the same 
and should be modified. Or there is hidden copy of this array? Or 
there is some policy in reference semantic - modifiable dynamic 
array and read only - when it is copying in stack as function 
argument :)


More information about the Digitalmars-d-learn mailing list