C ++ reference
1. Concepts of reference
A reference is an alias of a variable (target). The referenced operation is exactly the same as the direct operation on a variable.
Declared method: type identifier & reference name = target variable name.
Note:
(1) & this is not an address calculation, but an identifier.
(2) type identifier refers to the type of the target variable.
(3) When declaring a reference, it must be initialized at the same time.
(4) After the reference declaration is complete, it is equivalent that the target variable name has two names, namely, the original name and reference name of the target, and the reference name cannot be used as the alias of other variable names.
Int a, & ra =;
A is the original name of the target, and ra is the reference name of the target. Assign a value to ra: ra = 1; equivalent to a = 1;
(5) declaring a reference is not a new variable. It only indicates that the reference name is an alias of the target variable name. It is not a data type, therefore, the reference itself does not occupy storage units, and the system does not allocate storage units to the reference. Therefore, finding the address for the reference is to find the address for the target variable. & Ra and &.
(6) The referenced array cannot be created. An array is a collection composed of several elements, so a collection composed of references cannot be created. However, you can create an array reference.
Ii. referenced applications
1. Function Parameters
An important role of reference is as a function parameter. When a function is called, the reference parameter does not generate a copy of the real parameter in the memory. The parameter is directly used to operate the real parameter, you need to allocate storage units to the parameters. The parameters are copies of the real variables. if the object is passed, the copy constructor is also called. Therefore, when the data transmitted by a parameter is large, it is better to use reference than to transmit a parameter using a common variable.
2. Frequent reference
Common Reference declaration method: const type identifier & reference name = target variable name;
The reference declared in this way cannot be modified by reference to the value of the target variable, so that the referenced target becomes const, achieving the security of reference.
3. Reference as return value
To return a function value by reference, the function must be defined in the following format:
Type identifier & function name (parameter list and type description) {function body}
Functions in C ++ return the following situations:
1) return non-reference type: the return value of the function is used to initialize the temporary object created when the function jumps out. The method for initializing a temporary object with function return values is the same as that for initializing a parameter with real parameters. If the return type is not a reference, the return value is copied to the temporary object in the returned part of the function. The returned value can be either a local object or an expression.
2) return reference: When the function returns the reference type, it does not copy the return value, but returns the reference of the object (that is, the object itself ).
Function return reference: it is actually the memory address of a variable. Since it is the memory address, it can certainly read and write the value of the memory region corresponding to the address, that is, the "Left value ", it can appear on the left of the value assignment statement.
When a function returns a reference, you can use a global variable (returned as a function), or a reference or pointer (returned as a function) in the form parameter table of the function, the returned reference is meaningful only when the variable still exists after the return is executed.
The reference must follow the following rules as the return value:
(1) References to local variables cannot be returned. The main reason is that local variables will be destroyed after the function returns, so the returned reference becomes a reference of "no finger", and the program enters the unknown state.
(2) You cannot return a reference to the memory allocated by the new function. Although there is no passive destruction of local variables, this situation (returning a reference to the memory allocated by the new function) faces other embarrassing situations. For example, if a reference returned by a function only appears as a temporary variable and is not assigned to an actual variable, the space pointed to by the reference (allocated by new) cannot be released, cause memory leak.
(3) You can return a reference to a class member, but it is best to use const. This principle can be referred to Item 30 of Objective C ++ [1. The main reason is that when an object attribute is associated with a business rule, its value assignment is often related to some other attributes or the state of the object, therefore, it is necessary to encapsulate the value assignment operation in a business rule. If other objects can obtain the non-constant reference (or pointer) of this attribute, a simple value assignment to this attribute will damage the integrity of business rules.
(4) References and reloads of some operators: stream operators <and>. These operators are often used consecutively. For example: cout <\ "hello \" <endl; therefore, the return value of these two operators should be a stream reference that still supports these two operators.
4. References and Polymorphism
References are another method that can produce polymorphism effects except pointers. This means that a base class reference can point to its derived class instance.
[Example ]:
Class;
Class B: public {......}
B B;
A & Ref = B; // use A derived class object to initialize A reference to A base class Object
Ref can only be used to access the members inherited from the base class in a derived class object. It is a base class reference pointing to a derived class. If A virtual function is defined in Class A, and this virtual function is rewritten in Class B, A multi-state effect can be generated through Ref.
Reference Summary
(1) In reference use, it is meaningless to simply give an alias to a variable. The purpose of reference is mainly used in function parameter transfer, solve the problem of poor transmission efficiency and space of large data or objects.
(2) passing function parameters by reference can ensure that no copies are generated during parameter transfer and improve the transfer efficiency. The use of const ensures the security of reference transmission.
(3) The difference between a reference and a pointer is that after a pointer variable points to an object, it indirectly operates on the variable it points. When pointers are used in a program, the program has poor readability. The reference itself is the alias of the target variable, and the reference operation is the operation of the target variable.
(4) When to use the reference. Stream operators <and>, return values of the value assignment operator =, parameters of the copy constructor, parameters of the value assignment operator =, and references are recommended in other cases.
Reference: http://www.cnblogs.com/jycboy/p/5184638.html