Summary of Reference knowledge
A. References
1. definition : is an alias of a variable (the target), the operation of the reference is exactly the same as the direct operation of the variable.
  Declarative method of reference: ra a
type identifier & = target variable name; example:
Here, "&" is the reference operator,andRa is the alias of the shaping variable a , so that the ra Operation is actually the operation of a , the assignment of RA is to the assignment of a . RA = 999; (a = 999;)
2. referenced address
# Take a with its alias RA address and output, will find that the two storage address is the same, so the operation of the ra is actually the a the operation.
3. references are alias constants
# Refer to this alias is specifically for the name of a variable, the reference is affected by the original variable is also affected, are assigned together.
4. referencing Objects
# You can define an alias for an object, such as:
Human Mike;
Human &rmike = Mike;
# But you cannot define an alias for a class, such as:
Human &rhuman = Human; ( x )
Because Human is a type, it does not have a specific memory address.
The reference to an object is the same as its use, using the class's member operator (. ) to access member data and methods.
Two Passing parameters
1. pass the parameter by value
such as swap function swap, by value from the main function to the execution function of the argument, just a copy of the parameter operation, after execution will release the results of the copy, the main function finally can not get the correct result.
2. press the pointer to pass the parameter
void swap (int *a,int *b)
Swap (&a, &b);
a b memory address indirect access a and b a and b The address of the, then in the function of the *a and *b a and b
3. by reference
void swap (int &a,int &b)
Swap (A, b);
4. return multiple values with the pointer
int func (int *a, int *b, int *c)
Func (&a, &b, &c);
5. returning multiple values with a reference
int func (int &a, int &b, int &c)
Func (A, B, c);
three. Passing Objects
1. passing objects with values
Passing an object by value, if the object has more data, the memory overhead of such a copy is very large and undesirable.
2. passing objects with pointers
Iint Main ()
{
A;
Func (&a);
return 0;
}
A *func (a *one)
{
return one;
}
3. using the const pointer to pass an object
Using pointers to pass objects, although you can avoid invoking copy constructors and destructors, but because it gets the memory address of the object, you can modify the object's data at any time, so it actually destroys the protection mechanism by value.
However, we still have a workaround for this, which is to pass the object with a const pointer, which prevents any attempt to manipulate the object's data and guarantees that an object that is not modified is returned.
Const a *const func (const a *const one)
{
Cout<<one->get () <<endl;
return one;
}
4. using references to pass objects
A &func (a &one)
{
Cout<<one.get () <<endl;
return one;
}
5. the difference between a reference and a pointer
# The pointer can be empty, the reference cannot be empty;
# pointers can be assigned values, references cannot be assigned;
# The pointer can point to space in the heap, and references cannot point to space in the heap.
# The pointer points to a piece of memory whose contents are the address of the referred memory, while the reference is the alias of a block of memory.
# references can only be initialized once at the time of definition, immutable, pointers variable, reference "mindedness", pointers can be "inconstant".
#Reference NoConst, the pointer hasConst,Constthe pointer is immutable;Specifically, there is no int& const A in this form, and the const int& A is, the former guideline with itself that alias can not be changed, which is of course, so do not need this form, the latter refers to the value of the reference can not be changed).
#" sizeof reference "Gets the variable that is pointing to ( object ) , and the size of the " sizeof pointer " get the size of the pointer itself .
# pointer and reference self-increment (+ +) The meaning of the operation is different .
# references are type-safe, and pointers are not ( references are more type-checking than pointers ).
Four. Citation Summary
(1) In the use of reference, it is meaningless to give the individual name to a variable, and the purpose of the reference is to solve the problem of the transfer efficiency and the space of the large data or object in the function parameter passing.
(2) By using the parameters of the reference transfer function, we can ensure that the parameters are not generated in the transfer, improve the efficiency of the transfer, and ensure the security of the reference passing through the use of const .
(3) the difference between a reference and a pointer is that the pointer is indirectly manipulated by the variable it points to after it points to an object through a pointer variable. The use of pointers in the program, the readability of the program, and the reference itself is the alias of the target variable, the operation of the reference is the operation of the target variable.
(4) time to use references. The stream operator << and >>, the return value of theassignment operator = , the parameter of the copy constructor, the assignment operator = references are recommended in the parameters and other cases.
Summary of Reference knowledge