C ++ returns the referenced function.

Source: Internet
Author: User

C ++ returns the referenced function.

To return a function value by reference, the format of the function definition is as follows:

  Type identifier & type name (parameter list and type description)

{Function body}

  The const statement is as follows:

  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, ensuring the security of the reference.

  Notes:

  • To return a function value by reference, you must add & before the function name when defining the function &.
  • The biggest benefit of returning a function value with reference is that a copy of the returned value is not generated in the memory. When a function value is returned using a common return value method, you need to create a temporary variable in the memory. When the called function returns, copy the function value to the temporary variable. The main function is then computed based on the value of the temporary variable.
  • In the use of references, it is meaningless to simply give an alias to a variable. The purpose of reference is to solve the problem of large object transfer efficiency and space unsatisfactory in function parameter transfer.
  • The parameters of the referenced function can ensure that no copies are generated during parameter transmission and improve the transfer efficiency. The use of const ensures the security of reference transmission.
  • The difference between references and pointers is that: After a pointer variable points to an object, it indirectly operates on the variable it points to. In the program, the pointer is used to make the program readable, and the reference itself is the alias of the target variable, the referenced operation is the operation on the target variable.
  • Generally, the left side of the value assignment expression can only be the variable name, that is, the object to be assigned must be a variable, because the child has a variable to be assigned a value, and the constant or expression cannot be assigned a value, however, if the return value of a function is a reference, the left side of the value can be the call of the function.

I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!

Example:

  • Define a common function, return the function value using the return value method, and define another function to return the function value using the referenced method.

 

1 # include <iostream> 2 using namespace std; 3 4 5 int f1 (int a); 6 int & f2 (int a); 7 8 int f1 (int) 9 {10 int num; 11 num = a * 10; 12 return num; 13} 14 15 int & f2 (int a) 16 {17 int num; 18 num = a * 20; 19 return num; 20} 21 22 int main () 23 {24 int a, B; 25 a = f1 (1 ); 26 B = f2 (2); 27 cout <f1 (4) <endl; 28 cout <f2 (5) <endl; 29 cout <a <endl; 30 cout <B <endl; 31 // int & ra = f1 (3); this usage may fail, (different C ++ systems have different rules) 32 int & rb = f2 (3); 33 cout <rb <endl; 34 return 0; 35}

 

 

 

  • Use the referenced function value as the left value of the value assignment expression.
 1 #include<iostream> 2 using namespace std; 3  4 int &put(int n);  5 int vals[10]; 6 int error=-1; 7  8  9 int &put(int n)10 {11     if(n>=0 && n<=9)12         return vals[n];13     else14     {   15         cout << "error!" << endl;16         return error;17     }   18 }19 20 int main()21 {22     put(0)=10;23     put(9)=20;24     cout << vals[0] << endl;25     cout << vals[9] << endl;26     return 0;27 }

 

 

  • Use const to limit reference
1 include <iostream> 2 using namespace std; 3 4 int & fn (const int & a) 5 {6 // a = 32; error! You cannot assign 7 int B = a; 8 return B; // if a is returned, an error 9} 10 11 int main () is returned () 12 {13 int & a = fn (32); 14 cout <a <endl; 15 return 0; 16} 17 18 // cainiao. Please criticize and advise, coding habits and specifications !!! Thank you !!!

 

 

 

I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!

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.