Re: reference type methods
I think you can simplify things with references a lot if you realize
that there is no significant difference between references and pointers.
For example
int iX = 10;
int& rX = iX;
int* pX = &iX;
In both cases we save an address of the variable iX in a reference or in
a pointer. So
rX = 5; // This makes the expression iX == 5 true
*pX = 6; // This makes the expression iX == 6 true
Then think about the function
int* fPointer() { return &iX; }
By all means you realize that
*fPointer() = 5; // This makes the expression iX == 5 true;
Why are you surprized that the following is correct?
int& fRef() { return iX; }
fRef() = 6; // This makes the expression iX == 6 true;
There is no difference between rX and fRef() in this case. fRef returns
not the rValue but the lValue which can be modified (the same way as the
pointer value can be used to modify the value it is referencing to).
Now if you accept that a[n] = 5 puts the value 5 into the n+1's element
of the array you should accept the syntax below:
int& f( int n ) { return a[n]; }
f(n) = 5;
Obviously f(n) and a[n]is just the same thing. You can look on the
pointers if you want, it is just the same:
int* f( int n ) { return a + n; } // &a[n] and a + n is the same
*f(n) = 5;
P.S.
a[5] cannot be an r-value. It is an l-value. Otherwise a[5] = 6 is a
syntax error.