function reference

1. Reference review

quote

    Give the object another name (reference is an alias)
1. Constants (literal values) cannot be directly referenced
double & d = 12.3; //Error
reason: d = 111; that is, the value originally referenced by d is changed. Violates the basic concept of constants

If you want to reference:
//Reference to constant – legal
const double & d = 12.3;

int int_value = 1024;

//refValue指向int_value,是int_value的另一个名字
int & refValue  = int_value;

//错误 :引用必须被初始化
int & refValue2;

Using references does not require testing their validity , so using references is more efficient than pointers.

Note:
1. A reference is not an object, it is just an alias for an existing object.
2. A reference is closer to a const pointer. Once it is associated with a variable, it will always be loyal to it.
3. When using reference variables as parameters, the function will use the original data instead of a copy.
4. When the memory occupied by the data is relatively large, it is recommended to use reference parameters.

Code example
Reasons for using references:
    1. It can make it easier to write code
    2. You can pass an object directly instead of copying the object. Note
1. Using const
    in reference parameters can avoid unintentional modification of parameters     2. Reference parameter suggestions Use const whenever possible

2. Return reference

1. Function return reference type

Note
    1. Do not return references to local variables.
    2. The function does not need to return a value. When it does not return a value, it will return the passed reference object itself by default.
    3. When returning a reference, the function parameters are required to contain the returned reference object.

explain:

   1. Do not return references to local variables

#include <iostream>

using namespace std;

int & sum(){
    int num = 10;
    int & rNum = num;
    return rNum;//f返回了一个局部变量的引用类型变量
}
/**
注意:rNum是在sum()中定义的,所以叫局部变量
    rNum的生存期只在sum()中!!

    函数中的局部变量会被内存回收,
    所谓的内存回收,并不是把内存保存的设置清零,
    而是指内存中你的程序申请的这块内存已经不是你的了!
*/
void test(){
    int x = 1;
    int y = 2;
}

int main()
{
	//result在这里引用了一个局部变量
    int & result = sum();
    test();
    cout << "result = " << result << endl;
    return 0;
}
输出结果:
result = 2

Analysis:
The memory of local variables in sum is recycled and reused, resulting in reassignment by other objects .

   2. Functions cannot return values. By default, they return the passed-in reference object itself.

#include <iostream>

using namespace std;

int & sum1(int & num1,int &num2){
    num1++;
    num2++;
}

int & sum2(int & num1,int &num2){
    num1++;
    num2++;
    return num1;
}
int & sum3(int & num1,int &num2){
    num1++;
    num2++;
    return num1 + num2;	//此句报错,相当于返回第三个未定义的引用变量
    									//未返回函数中包含的引用对象
}
int main()
{   int x = 10;
    int y = 20;
    int & result1 = sum1(x,y);
    cout << "result1 = " << result1 << endl;        //21
    int & result2 = sum2(x,y);
    cout << "result2 = " << result2 << endl;        //12
    return 0;
}
结果:
result1 = 21
result2 = 12

Explanation: When a reference does not return a value, the last reference itself is returned by default.

   3. When returning a reference, the function parameters are required to contain the returned reference object.

Explain sum3() in 2

2. Return the lvalue and rvalue in the reference

int & sum4(int & num){
    return num;
}

int main()
{
    int n = 10;
    int numX = 15;
    int & result4 = sum4(numX);
    sum4(numX) = 55;
    cout << "result4 = " << result4 << endl;        //55
结果:
result = 55

sum4(numX) = 55 can be seen as sum4(numX) is a reference to 55

Suggested solutions:
    1. Change the return value type to const int &

const int &sum(int & num){
	//......
}

    2. The const type is an lvalue that cannot be modified.
sum(num) = 55; will be illegal
    3. Omitting const will make the meaning of the function more ambiguous. It is recommended to avoid ambiguity in the design function, because ambiguity will increase the chance of making mistakes, and mistakes should be avoided as much as possible.

lvalue and rvalue

3. Summary of using reference parameters

Insert image description here

Guess you like

Origin blog.csdn.net/X_King_Q/article/details/104467174