When you use a reference at the parameter, it gets bound to the outside variable that gets passed in. Namely, in our example, passByRef gets bound to variable. Modifications are made up the same address.
A reference can only be bound ONCE.
Thus, once it gets bound, any modification to that reference takes place outside as well.
So for any assignments inside method test, it happens to variable on the outside as well.
If you try to rebind the reference doing something like:
1 2 |
int anotherVar = 243; &passByRef = anotherVar; |
you’ll get an reassignment error.
1 2 3 4 5 6 7 8 9 10 11 |
//the structureByRef gets bind to the outside variable being passed in -(void)test:(int&)passByRef { DLOG(@"%p", &passByRef); passByRef = 66; int anotherInt = 243; passByRef = anotherInt; passByRef = *new int(6680); } |