(1) What is reference?
A reference is the alias of a variable. A reference to a variable is equivalent to an operation variable, which is similar to a pointer. However, an operation reference does not use an address symbol like an operation pointer, inconvenient. The Operation Reference is the same as the operation of common variables, so reference is more encouraged in C ++.
(2) Why should C ++ functions use references?
The C language uses pointers in large quantities as input parameters or function return values. This is because copying values consumes a lot (for example, passing in a large struct ). Therefore, using references in C ++ as function parameters and return values is the same purpose as using pointers. In addition, the form is more intuitive, So C ++ advocates the use of references.
(3) C ++ returns a reference?
The advantages of using references as function parameters and return values are described above.
Functions in C ++ return the following situations:
1) Return Value of main: 0 indicates that the operation is successful.
2) 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.
3) 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.
1) 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.
Exp1:
View Code
1 #include <iotream>
2
3 int global_variable; //global variable
4
5 int &f(int t1, int t2) //return the reference of the global variable "global_variable"
6
7 {
8
9 global_variable = t1 + t2;
10
11 return global_variable;
12
13 }
14
15 int main(void)
16
17 {
18
19 int f = f(1, 3);
20
21 std::cout << global_variable << std::endl; //L1
22
23 f(2, 8)++;
24
25 std::cout << global_variable << std::endl; //l2
26
27 f(2, 3) = 9;
28
29 std::cout << global_variable << std::endl; //L3
30
31 return 0;
32
33 }
L1: If f (1, 3) is executed, global_variable is changed to 4.
L2: If f () ++ is executed, f () is 10. You can execute ++ (indicating that it is the left value) and change it to 11.
L3: Execute f (2, 3) and change global_variable to 5. Because it is the left value, the value of global_variable is changed to 9.
Exp2:
View Code
#include <iostream>
int &t(int a, int b, int &result)
{
result = a + b;
return result;
}
int &f(int a, int b, int *result)
{
*result = a + b;
return *result;
}
int main(void)
{
int t_variable;
int *f_variable = new int;
t(1, 2, t_variable)++;
std::cout << t_variable << std::endl; //t_variable = 4
f(1, 2, f_variable)--;
std::cout << *f_variable << std::endl; //t_variable = 2
delete f_variable;
return 0;
}
Exp3:
Int & t (int a, int B)
{
Return a + B; // now allowed
}
2) because the returned variable address still exists when the function exits during the life cycle, any operation on the return value of the function is equivalent to the operation on the variable pointed to by the variable address. If const is added before the return value, this variable cannot be modified.
View Code
#include <iostream>
const int &t(int a, int b, int &result)
{
result = a + b;
return result;
}
int main()
{
int t_variable;
t(1, 2, t_variable);
std::cout << t_variable << std::endl; //t_variable = 3
t(1, 2, t_variable)++;
std::cout << t_variable << std::endl; //not allowed
}
Summary:
(1) Using references as function parameters and return values is more efficient.
(2) The object reference returned by the function must exist before calling the function. The reference of a local variable cannot be returned!
(3) When you do not want the returned object to be modified, you can add a const.