References as function parameters and references as function return values

Part of the reference blog: http://t.csdn.cn/b5x99

References as function parameters

Function: When a function passes parameters, you can use the reference technique to let the formal parameters modify the actual parameters

Advantages: can simplify the pointer to modify the actual parameter

Value passing: when the function calls the actual parameter, the value is passed to the formal parameter, and the formal parameter cannot modify the actual parameter when the value is passed

Address passing: When the function is called, the actual parameter passes the address to the formal parameter

//1. 值传递
void mySwap01(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

//2. 地址传递
void mySwap02(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

//3. 引用传递
void mySwap03(int& a, int& b) {//注意这里,传参数时就是对引用的初始化
    int temp = a;
    a = b;
    b = temp;
}

int main() {

    int a = 10;
    int b = 20;

    mySwap01(a, b);
    cout << "a:" << a << " b:" << b << endl;

    mySwap02(&a, &b);
    cout << "a:" << a << " b:" << b << endl;

    mySwap03(a, b);
    cout << "a:" << a << " b:" << b << endl;

    system("pause");
    return 0;
}

 Summary: The effect of passing parameters by reference is the same as passing by address. The syntax for quoting is clearer and simpler

function return value by reference

A reference can exist as the return value of a function. When a reference is used as a return value of a function, you must add & before the function name when defining the function

The following is a comparative analysis of the four situations

There are two functions fn1 and fn2, temp is a global variable

//代码来源:RUNOOB
#include<iostream>
using namespace std;


float temp;//全局变量

float fn1(float r){
    temp=r*r*3.14;
    return temp;
} 

float &fn2(float r)//&说明返回的是temp的引用,换句话说就是返回temp本身
{ 
    temp=r*r*3.14;
    return temp;
}


int main(){
    float a=fn1(5.0); //case 1:返回值

    //float &b=fn1(5.0); //case 2:用函数的返回值作为引用的初始化值 [Error] invalid initialization of non-const reference of type 'float&' from an rvalue of type 'float'
                           //(有些编译器可以成功编译该语句,但会给出一个warning) 

    float c=fn2(5.0);//case 3:返回引用

    float &d=fn2(5.0);//case 4:用函数返回的引用作为新引用的初始化值

    cout<<a<<endl;//78.5
    //cout<<b<<endl;//78.5
    cout<<c<<endl;//78.5
    cout<<d<<endl;//78.5

    return 0;
}

case 1: call the function with return value

When returning the value of the global variable temp, C++ will create a temporary variable in memory and copy the value of temp to the temporary variable . After returning to the main function main, the assignment statement a=fn1(5.0) will copy the value of the temporary variable to the variable a

case 2: Call the function by initializing the reference with the return value of the function

In this case, the function fn1() returns by value. When returning, first copy the value of temp to the temporary variable . After returning to the main function, use the temporary variable to initialize the reference variable b , so that b becomes the alias of the temporary variable . Because the scope of temporary variables is short (in the C++ standard, the life cycle of temporary variables or objects ends after a complete statement expression ends, that is, after the statement float &b=fn1(5.0);), and b It is an alias for a temporary variable, so b will be in danger of being invalid , and it is very likely that the value in the future will be an undetermined value.

case 3: Call the function by returning a reference

In this case, the function fn2() is returned by reference, and it returns temp itself . It does not generate a copy, but directly returns the variable temp to the main function, that is, the lvalue in the assignment statement of the main function. It is directly copied from the variable temp (that is to say, c is just a copy of the variable temp rather than an alias ), thus avoiding the generation of temporary variables . Especially when the variable temp is an object of a user-defined class, this also avoids the process of calling the copy constructor in the class to create a temporary object in memory, which improves the efficiency of the program's time and space usage

case 4: Call the function by using the reference returned by the function as the initialization value of the new reference

In this case, the return value of the function fn2() does not generate a copy, but directly returns the variable temp to the main function. In the main function, a reference declaration d is initialized with the return value, that is to say, d becomes an alias of the variable temp at this time. Since temp is a global variable, temp will always remain valid during the validity period of d, so this approach is safe.

Summary: The biggest advantage of using references as the return value of a function is that no copy of the return value is generated in memory.

Also need to be careful not to return local variable references

//返回局部变量引用
int& test01() {
	int a = 10; //局部变量
	return a;
}

//返回静态变量引用
int& test02() {
	static int a = 20;//全局区
	return a;
}

int main() {

	//不能返回局部变量的引用
	int& ref = test01();
	cout << "ref = " << ref << endl;//ref=10
	cout << "ref = " << ref << endl;//乱码

	//如果函数做左值,那么必须返回引用
	int& ref2 = test02();
	cout << "ref2 = " << ref2 << endl;
	cout << "ref2 = " << ref2 << endl;

	test02() = 1000;

	cout << "ref2 = " << ref2 << endl;
	cout << "ref2 = " << ref2 << endl;

	system("pause");

	return 0;
}

Call the test01 function, the first output is normal, and the second output is garbled

Also, if the return value of the function is a reference, the function call can be used as an lvalue.

Guess you like

Origin blog.csdn.net/hbzdsXCV/article/details/128355689