A function parameter is either
by value or
by reference.
A by value parameter is a copy made from whatever the calling code calls the function with.
A reference is a reference, not a copy.
Lets start abstract:
1 2 3 4 5 6 7
|
void foo( T bar, U & gaz );
int main() {
T x;
U y;
foo( x, y );
}
|
The bar is a by value parameter. The bar is copy constructed from x. It is similar to writing:
T bar = x;
Changing bar within foo will not change the x in main.
Tha gas is a reference to y (during this call to foo). If you do change the gaz in the function, then the value of y will change too.
Same example, with more concrete types:
1 2 3 4 5 6 7
|
void foo( int * bar, int & gaz );
int main() {
int * x;
int y;
foo( x, y );
}
|
The bar is a copy. If you do change what the bar points to, the change will not affect the x.
What does confuse you (and many other too) is that the object that the x and bar do point to, can be changed via both pointers. That object is not x.
One more time:
1 2 3 4 5 6 7 8
|
void foo( int * * bar, int * & gaz );
int main() {
int z;
int * x = &z;
int ** w = &x;
foo( w, x );
}
|
Now bar is a copy of w, which is a pointer to x. If you do change the pointed to object (*bar) in the function, then you do change x.
However, the gaz is now a reference to x, so changing gaz has same effect as changing *bar.