Reference as function return value

Source: Internet
Author: User

"Reference as a function parameter" and "reference as a function return value" I. There are two reasons for reference as a function parameter: 1. Modify the parameters in the function. 2. Improve the function call and running efficiency. For the first point, we all know that the parameters and real parameters mentioned in C ++ will be mentioned in the function. If the function parameter is essentially a form parameter, but the scope of the form parameter is only within the function body, that is to say, the real parameter and the form parameter are two different things, to replace the real parameter, there must be a value transfer. When a function is called, the value transfer mechanism creates a copy of the real parameter by assigning values to the form parameter through "parameter = real parameter. Even if the parameter is modified inside the function, the parameter is only for the form parameter, that is, the copy. The real parameter will not be changed. Once the function is completed, the life of the form parameter is terminated, and the modifications made do not affect any variables. For example, void swap (int p1, int p2) // exchange two variables. Here, the function parameter is p1 and p2, and {int p; p = p1; p1 = p2; p2 = p;} void main () {int a, B; is not referenced; cin> a> B; // enter the value of swap (a, B) in the variables a and B ); // call the swap function cout directly using variables a and B as real parameters <a <''<B; // output result you will find that the output a and B are still the values you entered, and there is no exchange. If we change to: void swap (int & p1, int & p2) // exchange the two variables. In this example, the function parameter is p1, and p2 references {int p; p = p1; p1 = p2; p2 = p;} and the value is exchanged. The principle is that when & p1 and & p2 are used, p1 and p2 are real parameter aliases, just like a pointer pointing to real parameters. Changing p1 and p2 is to change the value of the real parameter. The second point can be analyzed in conjunction with the first point. p1 and p2 are references of real parameters without passing through the value transfer mechanism, and there is already information about the real parameter values. Therefore, there is no time and space consumption for transferring values and generating copies. This is necessary when the program requires high efficiency. ii. Description of reference as the function return value: (1) to reference and return the function value. to define a function, add a value before the function name. & (2) 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. Example: # include <iostream. h> float temp; // define the global variable tempfloat fn1 (float r); // declare the function fn1float & fn2 (float r); // declare the function fn2float fn1 (float r) // define the function fn1, which returns the function value {temp = (float) (r * 3.14); return temp;} float & fn2 (float r) using the return value method) // define the function fn2, which returns the function value {temp = (float) (r * 3.14); return temp;} void main () as a reference () // main function {float a = fn1 (10.0); // In case of 1st, the system generates a copy (that is, a temporary variable) float & B = fn1 (10.0 ); // In 2nd cases, errors may occur (different C ++ systems have different rules) // a temporary variable cannot be returned from the called Function Or the local variable reference float c = fn2 (10.0); // 3rd cases, the system does not generate a copy of the returned value // you can return a reference float & d = fn2 (10.0) from the called function; // 4th cases, the system does not generate a copy of the returned value. // you can return a reference cout <a <c <d;} reference from the called function as the returned value, the following rules must be observed: (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 with an actual variable, the space pointed to by this 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. 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.

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.