references and function calls
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Fri Mar 16 06:27:14 PDT 2007
You seem to have a misunderstanding on the semantics of 'out':
Derek Parnell wrote:
[removed irrelevant lines from below source]
> void funcOne(in int A, inout int B, out int C)
> {
> if (C > 0) // The value is not readable as 'out' always
> // initializes it before the function gets control.
> C = 3; // The value is changable and changes
> // go back to the caller.
> }
>
> int c = 9;
> funcOne( a,b,c)
> // c is still 9 ('out' prevented called func from seeing
> // its value at call time)
Wrong. c is now 0, because 'out' parameters are initialized at the start
of the function. When the condition was evaluated c == C == 0 so the
condition is false and c remains 0, *even after the function returns*.
> void funcTwo(in char[] A, inout char[] B, out char[] C)
> {
> // NB: When passing arrays, the value passed is the reference!
> if (C.length > 0) // The value is not readable as 'out' always
> // initializes it before the function gets control.
> C = "c".dup; // The value is changable and changes
> // go back to the caller.
> }
>
> char[] cc = "ccc".dup;
> funcTwo( a,b,c)
> // cc is still "ccc" ('out' prevented called func from seeing
> // its value at call time)
cc should now be null (.length == 0, .ptr == null) for similar reasons
as in the first example.
More information about the Digitalmars-d-learn
mailing list