C ++ tutorial (continuous serialization) -- use reference as the function return value

Source: Internet
Author: User

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} visible, to reference the returned function value, you must add & before the function name when defining the function &. When a function is referenced as a return value, the return value of the function can be understood as the fact that the function returns a variable. When the function returns a reference, it returns an implicit pointer to the return value, the referenced function can be used as the left operand of the value assignment operator. In addition, the biggest benefit of returning a function value with a reference is that a copy of the returned value is not generated in the memory. # Include <iostream> using namespace std; int & func () {static int num = 0; return ++ num;} void main () {int I; for (I = 0; I <5; I ++) cout <func () <'\ T'; cout <endl; func () = 10; for (I = 0; I <5; I ++) cout <func () <'\ T'; cout <endl ;} compile and run the above program, the output result 13.1 shows that the function func is used as the left operand of the value assignment operator. Figure 13.1 The output result of a program provides an example of returning a referenced function as the left value, which effectively reveals the essence of returning a referenced function. # Include <iostream> using namespace std; double array [5] = {100.1, 100.2, 100.3, 100.4}; double & change (int I) {return array [I];} int main () {int I; cout <"Original Value:"; for (I = 0; I <5; I ++) cout <array [I] <"; cout <endl; change (2) = 3.14; change (3) =-99.99; cout <" after modification: "; for (I = 0; I <5; I ++) cout <array [I] <"; cout <endl; return 0 ;} compile and run the above program. Please experiment with the output. The Return Value of the Function change is a double type reference, and the value is a reference of the elements in the array specified by its parameter I. Therefore, when the statement "change (2) = 3.14;" is executed in the main function, the change function returns a reference to the array element array [2. Through this reference, array [2] is assigned 3.14 values. The subsequent statement "change (3) =-99.99;" follows the same principle. Because change returns a reference to a specific element in the array, this function can be placed on the left of the value assignment statement to assign values to this array element. Note that there is no reference to the array, but it exists when it points to the reference of the array element. When you use a reference as the return value, you need to note some points. First, you cannot return a reference to a local or temporary variable, but you can return a reference to a global variable. That is to say, the referenced object cannot be out of scope. The main reason is that local variables will be destroyed after the function returns. Therefore, the returned reference becomes a reference with "no finger", which is not allowed. For example, the following code is incorrect: int & func () {int I = 10; return I;} in Visual C ++ 6.0, the above code will throw a warning "warning c00002: returning address of local variable or temporary" during compilation. Although the compiler does not report an error, this still means instability. In addition, when the above func () function is used as the left value in Visual C ++ 6.0, the result shows that the assignment is not successful, this also warns us that such usage should be absolutely forbidden. After all, when the function func () returns, the local variable I is out of scope. Therefore, the reference to I returned by func () is an undefined reference. In addition, Some compilers that support higher standard C ++ will report an error when making the left value of the above func! In addition, such problems may also be indirectly generated, and the errors appear more concealed and not easily discovered. Therefore, when a reference to an object is returned, check whether the object is out of scope. Second, the reference of the memory dynamically allocated within the function cannot be returned. Although the Passive Destruction of local variables does not exist, there are still some problems in this case. 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 allocated by the new resource pointed to by this reference cannot be released, this causes memory leakage. Finally, a reference to a class member can be returned, but it is better to be a const constant. This is because when an object attribute is associated with a service 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 uncommon reference of this attribute, a simple value assignment to this attribute will damage the integrity of business rules. --------------------------------------------------------------------

This article from the "White Horse negative gold ghost" blog, please be sure to keep this source http://baimafujinji.blog.51cto.com/907111/195792

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.