c ++ passing the value, site transfer, passed by reference

Detailed concept

1. The transmission value:

  The parameter is a copy of the argument, the change parameter value does not affect the value of the external argument.

  From the perspective of the called function, the value of a one-way transmission (argument -> parameter), the value of the parameter only incoming, outgoing not;

  When the need to modify the internal function parameters, and changing the undesirable effect on the caller, the value transfer.

2. pointer is passed

  Parameter argument is a pointer to the address pointer pointing to the parameter when the operation is equivalent to the operation of the argument itself

3. passed by reference

  Parameter is the equivalent of the argument of "Alias", the operation of the parameter is actually operating on the argument.

  In reference to the transfer process, the transfer function as parameters in the form of a local variable opened in the stack memory space, but then is stored into the address to the calling function argument variables.

  Is any operation parameter of modulation functions are treated as indirect addressing, i.e. access the argument in the calling function by the variable address stored in the stack.

  Because of this, any operation is made on the parameter adjustment function argument variables are influenced in the calling function.

Third, the comparison and pointer references

1. similarities

  Is the concept of address;

  A pointer to a memory, it is referred to the content of the memory address;

  It is a reference block of memory alias.

2. Different points

  Pointer is an entity, and the only reference is an alias;

  References only once during initialization is defined, then immutable; pointer variable at any time;

  const: reference only const int & a; (reference values ​​can not be changed points); no int & const a; (i.e., alias immutable reference itself, which is, of course, this form is not required); Pointer both;

  Null reference can not be empty, the pointer;

  Reference is type-safe, rather than the pointer; (refer to more than the pointer type checking)

  sizeof reference to the size of the variable pointed (object), sizeof pointer and a pointer size obtained is itself

Fourth, the application

Nature of reference passed as a pointer passed, but the value is transmitted as the writing.

Value is passed:

void Func1(int x)

{

  x = x+1;

}

Passing a pointer:

void Func1(int *x)

{

  *x = *x +1;

}

Passed by reference:

void Func1(int &x)

{

  x = x+1;

}

In fact, with the "reference" you can do anything "pointer" can do, but why should the "reference"?

->指针太灵活,可以毫无约束地操作内存中的任何东西,尽管指针功能强大,但是非常危险;

如果的确只需要借用下 某个 对象的 别名, 那么就用引用,以免发生意外。

 

以下代码很好的说明了问题

 1 #include<iostream>
 2  
 3 using namespace std;
 4  
 5 //值传递
 6  void change1(int n){
 7     cout<<"值传递--函数操作地址"<<&n<<endl;         //显示的是拷贝的地>   址而不是源地址 
 8     n++;
 9 }
10                                                                        
11 //引用传递
12 void change2(int & n){
13     cout<<"引用传递--函数操作地址"<<&n<<endl;
14     n++;
15 }
16  //指针传递
17 void change3(int *n){
18      cout<<"指针传递--函数操作地址 "<<n<<endl;
19     *n=*n+1;
20  }      
21 int     main(){
22     int n=10;
23     cout<<"实参的地址"<<&n<<endl;
24     change1(n);
25     cout<<"after change1() n="<<n<<endl;
26     change2(n);
27     cout<<"after change2() n="<<n<<endl;
28     change3(&n);
29     cout<<"after change3() n="<<n<<endl;
30     return true;
31 }

结果如下

qqtsj@qqtsj-Nitro-AN515-51:~/cpp$ g++ -o tate1 tate1.cpp
qqtsj@qqtsj-Nitro-AN515-51:~/cpp$ ./tate1
实参的地址0x7ffd1ee21884
值传递--函数操作地址0x7ffd1ee2186c
after change1() n=10
引用传递--函数操作地址0x7ffd1ee21884
after change2() n=11
指针传递--函数操作地址 0x7ffd1ee21884
after change3() n=12

 

Guess you like

Origin www.cnblogs.com/tanshengjiang/p/11831760.html