C ++ learning by referencing variables and learning by referencing variables
(1) reference variable (rv) is mainly used as a form parameter of the equation. rv can be used to directly operate the original data instead of copying the data, saving time and space, in particular, for a struct and a class object, it is a good choice to pass the value of common data.
(2) define a reference variable in the form of type & name. refer to the example in c ++ premier plus,
Int rats;
Int & rodents = rats; (int & means "reference to an int variable ")
A rodents is defined. It refers to the reference variable of rat. After initialization, rats and rodents have the same value and point to the same place, which can be used interchangeably, it is "one thing, two names ".
(3) The rv must be initialized during the Declaration. After initialization, the pointing point will not change, which is equivalent to a const type pointer. Subsequent assignments can only change the value but cannot change the address. For example
Int & rodents = rats; equivalent
Int * const pr = & rats;
(4) When the formal argument of a function is an rv pointing to the const type, the compiler will generate temporary variables in the following two cases,
When the real parameter type is correct, but it is not "Left value (lvalue )"
If the real parameter type is incorrect, it can be converted to the correct type.
(5) The C ++ 11 standard introduces a new syntax called "rvalue reference". Therefore, the original reference is now called "lvalue reference )"
(6)What is the difference between the two methods of function return reference and return value?
For the return method of "return value", the object after the return statement is copied to a temporary memory, then, the data in the temporary memory is copied to the corresponding object of the called function, but if it is "return reference", the object after the return statement is directly copied to the corresponding object of the called function, faster.
(7) What is the purpose of returning a const-type reference?
The const type can be used to return a reference to avoid modifying the return value.