In C ++, a reference parameter as a function

In C ++, a reference parameter as a function

It cited as an argument

The reason why the increase in C ++ reference type, it is mainly a function as a parameter in order to expand the charge transfer function data of the function.

————————————————————

c ++, transfer function parameters:
(1) the variable name as arguments and parameters. In this case the value is passed to the parameter variable transmission is unidirectional. If during the execution of the function of the parameter value changes, does not return to the argument. Because when calling the function, parameters, and arguments are not the same memory cell. // c with


(2) passing a pointer variable. The parameter is a pointer variable, the argument is the address of a variable, function call, parameters (pointer variable) to point argument variable unit. This value can be changed by parameter argument pointer. // c with


(3) C ++ provides a reference variable transmission. Parameter is a reference variable, and argument is a variable, the function is called, parameters (reference variable) to point argument variable unit. This argument may be changed by the value of parameter reference.

 

#include <iostream>
using namespace std;
void funcr(int &num , int x)
{
    cout<<"in funcr,addr is:"<<&num<<endl;//对比地址,等于原地址
    num = x ;//会改变传入参数的值。 
}

void func(int num , int x)//不能写成void funcr(int num , int x),不正确的重载,导致不知道该调用谁。
{
    cout<<"in func,addr is:"<<&num<<endl;//对比地址,有所变化
    num = x ;//只会在函数内改变拷贝变量的值,不会改变传入的参数值
} 

int main() {
    int x = 100;
    int &rx = x ;
    cout<<"addr_x:"<<&x<<endl;
    cout<<"addr_rx:"<<&rx<<endl;//变量地址和它的引用地址相同


    funcr(x,177);//改变传入参数的值,不管代码中传的是变量本身还是引用   
    cout<<"after funcr , x="<<x<<endl;

    funcr(rx,211);  //改变传入参数的值          
    cout<<"after funcr , rx="<<x<<endl;

    func (rx,233); //不会改变传入参数的值,不管代码中传的是变量本身还是引用                 
    cout<<"after func , rx="<<x<<endl;

    while(1);
    return 0 ;
} 
结果:
addr_x:0xbf82293c
addr_rx:0xbf82293c
in funcr,addr is:0xbf82293c
after funcr , x=177
in funcr,addr is:0xbf82293c
after funcr , rx=211
in func,addr is:0xbf822920
after func , rx=211

 

1. The value is passed: a copy of the parameter data to which the function belongs stack process, if the object is a class object value is passed a large structure or object will take some time and space.

2. passing a pointer: parameter also has a copy of the data to which the function belongs stack process, but a copy of the data is fixed as 4-byte address.

3. Referencing transmission: The same above-described data copy process, but it is for the address that corresponds to an alias address from the data resides.

In terms of efficiency, the pointer passed by reference, and the high transmission ratio of the transmission efficiency. The general idea of ​​using a reference on the transmission more compact, code logic clear.

Do function parameters passed by reference "is a C ++ features, C language does not support.

For example: the data structure and with & without &

With & parameter is a reference type, it is passed the address, in fact, participating with the change parameter is changed; without & parameter is a general parameter value is transferred, in fact, does not change as the reference parameter is changed . Therefore, structural changes, and the need to return this change of use Reference parameters, or with general parameters. GetElem (L, i) is simply to i-th element value, the structure of linear form is not any change, so do not add L & foregoing parameters. ListInsert (& L, i, e) are inserted into the linear list L of i-th element value of the element e of a linear list L configuration has changed, the length increases, so must be added before L &. If not, the display L, it does not show additional elements, L is the length of the display, is still increases from the previous value, 1 less than the actual length.

 

 

 

Published 18 original articles · won praise 86 · views 160 000 +

Guess you like

Origin blog.csdn.net/u013178472/article/details/104969327