Skyseraph
Source: http://www.cnblogs.com/skyseraph/
More highlights please visit Skyseraph personal site directly: http://skyseraph.com//
Related concepts
Three ways to transfer parameters in C + +
- Value of the passed parameter (value passed, referred to as value)
- The address of the pass parameter (called address transfer, referred to as a transmission)
- Reference passing (called a pass-through reference)
corresponding function invocation, address invocation and reference invocation
Formal parameters, actual parameters
When a function is defined, a parameter in the parameter table is called a formal parameter, or a parameter in the parameter table when the function call is called the actual parameter, referred to as the argument. Data transfer between an argument and a formal parameter is called a form-solid combination
Description
Typically, C + + is a call to a value, which is 单向
the only argument to the parameter. A formal parameter is essentially a copy of an argument, so the value of the external argument is not changed when it is passed
Meaning and function of the reference
A reference is an alias, and the function is mainly the parameter and return value of the transfer function.
Rules that are referenced
(1) The reference is created and must be initialized (the pointer can be initialized at any time).
(2) cannot have a null reference, the reference must be associated with a valid storage unit (the pointer can be null).
(3) Once a reference is initialized, the referenced relationship cannot be changed (unlike pointers, pointers can change the objects they refer to at any time.)
Instance
#include <iostream>using namespace std ;void Swap(int x, int y) ;int main(void){ int a = 1 ; int b = 2 ; cout << "a = " << a << ", " << "b = " << b << endl ; Swap(a, b) ; cout << "a = " << a << ", " << "b = " << b << endl ; system("pause") ; return 0 ;}
One: Value passing
void Swap(int x, int y) { int temp = x ; x = y ; y = temp ; }
Output Result:
A = 1, b = 2
A = 1, b = 2
Cause: the Swap (int x, int y) function takes a value pass, and the actual arguments passed in are actually copies of a and b rather than themselves, so changes to the copy do not reflect A and B.
II: Reference Delivery
void Swap(int &x, int &y){ int temp = x ; x = y ; y = temp ;}
Output Result:
A = 1, b = 2
A = 2, B = 1
Cause: the Swap (int x, int y) function takes the form of a reference pass, and the actual arguments passed in are actually references to A and B, and the changes to the reference directly reflect A and B.
Three: pointer passing
1. Change the pointer itself
void Swap(int *x, int *y){ int *temp = x ; x = y ; y = temp ;}
Calling method: Swap (&a, &b);
Output Result:
A = 1, b = 2
A = 1, b = 2
Cause: the Swap (int x, int y) function takes a pointer pass, and the actual argument passed in is actually a copy of the pointer to A and B, and it changes the copy itself rather than its indirect reference, so it does not affect the value that the pointer points to, A and B.
2. Changing the pointer's indirect reference
void Swap(int *x, int *y){ int temp = *x ; *x = *y ; *y = temp ;}
Calling method: Swap (&a, &b);
Output Result:
A = 1, b = 2
A = 2, B = 1
Cause: the Swap (int x, int y) function takes a pointer pass, although the incoming argument is also a copy of the pointer to a and B, but changes the indirect reference to the copy, either the pointer itself or its copy, pointing to the same value, so the change will reflect A and B.
You can view the disassembly code further, analyze and compare its directives
Summary: Pass reference = Pass pointer
Resources
Http://www.cnblogs.com/Jamedy/archive/2007/03/25/687150.html
Http://myturn.blog.hexun.com/15584978_d.html
http://lovemelovemydogs.blog.163.com/blog/static/9778560200721012092/
Reproduced C + + transfer value, pass reference, pass pointer