In C ++, calling a function involves the following processes:
1. Import the status of the parent function to the stack.
2. The system requests a memory segment as the stack space of the sub-function.
3. Call the copy constructor of the sub-function parameters, and the new object is pushed into the stack of the sub-function.
Of course, there are still many processes in it, which are not described in detail here.
Therefore, when the sub-function modifies the value of the function parameter, the value in the parent function is not modified. (Because it is modified to copy the new object after the construction, it is not the original object)
This is the value transfer.
Void fun (int B) {B = 10;} int main () {int A = 5; fun (a); // at this time a = 5 ;}
When the parameter declaration of your sub-function is a reference object,
3. The process is the process of value assignment. For example, Int & B =;
At this time, A and B are equivalent to establishing a co-existence relationship. (In layman's terms)
When B is passed to a subfunction, the value of B is modified in the subfunction, and the value of A in the parent function is also modified. (Because there is a reference to this layer)
This is to pass the reference.
Void fun (Int & B) {B = 10;} int main () {int A = 5; fun (a); // at this time a = 10 ;}
Understanding of function value passing and reference passing