C++ 引用调用和值调用的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2278700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:50:35  来源:igfitidea点击:

Difference between call by reference and call by value

c++c

提问by swapnadip

Possible Duplicate:
Difference between value parameter and reference parameter ?

可能的重复:
值参数和参考参数之间的区别?

What is the difference between call by reference and call by value?

按引用调用和按值调用有什么区别?

回答by Jerry Coffin

In C, there is no call by reference. The closest you can get is taking an address, and passing a copy of that address (by value -- see below).

在 C 中,没有引用调用。您可以获得的最接近的是获取一个地址,并传递该地址的副本(按值 - 见下文)。

In C++, call by reference passes a reference to an object (an alias for the original object). Generally this will be implemented as the object's address, though that's not guaranteed.

在 C++ 中,按引用调用传递对对象的引用(原始对象的别名)。通常这将作为对象的地址来实现,但不能保证。

Call by value means taking a value of some sort, and passing a copy of that value to the function.

按值调用意味着获取某种类型的值,并将该值的副本传递给函数。

The basic difference is that when you pass a parameter by value, the function receives only a copyof the original object, so it can't do anything to affect the original object. With pass by reference, it gets a reference to the original object, so it has access to the original object, not a copy of it -- unless it's a const reference, it can modify the original object (for one example).

基本的区别在于,当你按值传递参数时,函数只接收原始对象的副本,因此它不能做任何影响原始对象的事情。通过引用传递,它获得对原始对象的引用,因此它可以访问原始对象,而不是它的副本——除非它是一个常量引用,否则它可以修改原始对象(例如)。

回答by Alok Singhal

Briefly, call by referenceis when a function can modify its arguments:

简而言之,通过引用调用是指函数可以修改其参数:

def f(x):
    x := x + 1
    print(x)

x := 1
f(x)
/* Now, x is 2 */
print(x)

In "call by reference", the above will print 2twice.

在“call by reference”中,上面会打印2两次。

Call by valueis when a function receives a copy of the argument passed to it, so any modifications don't get seen by the caller. With f(x)defined as above, call by value would be:

按值调用是指函数收到传递给它的参数的副本,因此调用者看不到任何修改。与f(x)如上述所定义,通过值呼叫将是:

x := 1
f(x)
/* x is 1 in the caller */
print(x)

In the above, inside f(x), the print call would print 2, but that only modifies the copy of xthat f()got, so in the caller, xis still one, and the second print()prints 1.

在上文中,内f(x),打印通话将打印2,但只修改的副本xf()GOT,所以在调用者,x仍然是一个,第二个print()版画1

回答by Vinay Pandey

The arguments passed to function can be of two types namely 1. Values passed 2. Address passed

传递给函数的参数可以是两种类型,即 1. 传递的值 2. 传递的地址

The first type refers to call by value and the second type refers to call by reference. For instance consider program1

第一种是按值调用,第二种是按引用调用。例如考虑程序1

    main()
    {
        int x=50, y=70;
        interchange(x,y);
        printf(“x=%d y=%d”,x,y);
    }

    interchange(x1,y1)
    int x1,y1;
    {
        int z1;
        z1=x1;
        x1=y1;
        y1=z1;
        printf(“x1=%d y1=%d”,x1,y1);
    }

Here the value to function interchange is passed by value.

这里函数交换的值是按值传递的。

Consider program2

考虑程序2

main()
{
    int x=50, y=70;
    interchange(&x,&y);
    printf(“x=%d y=%d”,x,y);
}

interchange(x1,y1)
int *x1,*y1;
{
    int z1;
    z1=*x1;
    *x1=*y1;
    *y1=z1;
    printf(“*x=%d *y=%d”,x1,y1);
}

Here the function is called by reference. In other words address is passed by using symbol & and the value is accessed by using symbol *.

这里的函数是通过引用调用的。换句话说,地址是通过使用符号 & 传递的,而值是通过使用符号 * 来访问的。

The main difference between them can be seen by analyzing the output of program1 and program2.

通过分析program1和program2的输出可以看出它们之间的主要区别。

The output of program1 that is call by value is

按值调用的 program1 的输出是

x1=70 y1=50
x=50 y=70

But the output of program2 that is call by reference is

但是通过引用调用的 program2 的输出是

*x=70 *y=50
x=70 y=50

This is because in case of call by value the value is passed to function named as interchange and there the value got interchanged and got printed as

这是因为在按值调用的情况下,值被传递给名为交换的函数,在那里值被交换并打印为

x1=70 y1=50

and again since no values are returned back and therefore original values of x and y as in main function namely

再次因为没有返回值,因此 x 和 y 的原始值与主函数中的一样

x=50 y=70 got printed.

回答by ratty

Call by value:

1
2
3
4
5
6
7
  void foo( int x ) {
      cout << x << endl;
      x = 4;
  }

  int someVar = 5;
  foo( someVar );


The value 5 is pushed onto the stack and foo is called. Inside foo(), 5 is popped off the stack and output. x = 4 modifies the stack copy, which is then thrown away when the function returns.

1
2
3
4
5
6
7
  void foo( int& x ) {
      cout << x << endl;
      x = 4;
  }

  int someVar = 5;
  foo( someVar );


The address of someVar is pushed onto the stack and foo is called. Inside foo, the address is popped off the stack and the integer stored at that address is output. x = 4 modifies the memory referred to by the address, which means that when foo() returns, someVar now has the value 4.

Contrast the above code to

1
2
3
4
5
6
7
  void foo( int* x ) {
      cout << *x << endl;
      *x = 4;
  }

  int someVar = 5;
  foo( &someVar );


This code does the exact same thing as my reference example, it's just the syntax is a bit different: note the &someVar where foo is called, and note the *x in foo() to refer to the integer value.

回答by zabulus

In call by reference you will get the instance of a variable. After changing parameters of function, in this case your variable will change in call method. In call by value you will get the copy of the instance. And in this case changes of parameter variable will not influence on variable that was in call method

在按引用调用中,您将获得一个变量的实例。更改函数参数后,在这种情况下,您的变量将在调用方法中更改。在按值调用中,您将获得实例的副本。在这种情况下,参数变量的变化不会影响调用方法中的变量