There are three parameters passed in C + +: Passing the parameter value, passing the parameter address, and passing the reference to the parameter.
First, concept decomposition
1. Value
2. Pointers
A pointer is essentially a variable that holds the address of a variable, logically independent, and can be changed, including the change in the address it points to and the data it points to in the address.
3. References
A reference is an alias, logically not independent, its existence is dependent, so the reference must be initialized at the beginning, and its referenced object (that is, cannot be re-assigned, can only be attached to the same variable from beginning to end) in its life cycle.
Second, the concept of detailed
1. Value passing:
The parameter is a copy of the argument, and changing the value of the parameter does not affect the value of the external argument.
From the point of view of the called function, the value pass is one-way (argument-and-parameter), the value of the argument can only be passed in, not outgoing;
The value is passed when the parameter needs to be modified inside the function and you do not want the change to affect the caller.
2. Pointer passing
A parameter is a pointer to an actual parameter address, which is equivalent to the action of the argument itself when it points to the parameter
3. Reference delivery
The formal parameter is equivalent to the "alias" of the argument, and the operation of the formal parameter is actually the operation of the argument.
In the process of reference passing, the formal parameters of the called function open up the memory space in the stack as a local variable, but the address of the argument variable that is put in by the key function is stored.
Any operation of the modulated function on the formal parameter is handled as an indirect addressing, that is, the argument variables in the central Melody function are accessed through the address stored in the stack.
Because of this, any action taken by the modulated function on the parameter affects the argument variables in the keynote function.
Iii. Reference and pointer comparison
1. The same point
Are the concept of addresses;
The pointer points to a piece of memory whose contents are the address of the referred memory;
A reference is an alias for a block of memory.
2. Different points
The pointer is an entity, and the reference is only an individual name;
A reference can be initialized only once at the time of the definition, and the pointer is mutable at any time;
Const: The reference is only const int& A; (the value that the reference points to cannot be changed); there is no int& const A; (the reference itself is an alias immutable, which is of course, so it is not required);
The reference cannot be null, the pointer can be empty;
References are type-safe and pointers are not; (references are more type-checked than pointers)
sizeof refers to the size of the variable (object) to which it is pointing, and the sizeof pointer gets the size of the pointer itself.
Iv. Application
The nature of a reference pass is passed like a pointer, but the writing is passed as a value.
Value passing:
void Func1 (int x)
{
x = x+1;
}
Pointer passing:
void Func1 (int *x)
{
*x = *x +1;
}
Reference delivery:
void Func1 (int &x)
{
x = x+1;
}
In fact, anything "pointers" can be done with "references", but why do they have to be "referenced"?
Pointers are too flexible to manipulate anything in memory without restraint, although pointers are powerful, but very dangerous;
If you really only need to borrow an alias for an object, use a reference to avoid an unexpected occurrence.
Reference:
1. http://blog.csdn.net/htsnoopy/article/details/7070819
2. http://blog.csdn.net/zzuqqiu/article/details/5617263
3. http://blog.csdn.net/xiaocai0807/article/details/6526672
Value passing pointer passing and reference passing in C + +