简体   繁体   English

重访按值致电与按引用通话

[英]Call-By-Value vs Call-By-Reference revisited

let me first say. 我先说 I know the title suspects that I'm asking a question which was answered here and around the internet many times. 我知道标题怀疑是我在问一个问题,这里和互联网上已经多次回答了这个问题。 I did research indeed yet I just can't find a satisfying answer. 我确实做了研究,但找不到满意的答案。

My question in the end comes down to this. 最后我的问题归结于此。 Why is Java Call By Value and C++ Call By Reference (when using pointers) ? 为什么使用值按Java调用和按引用按C ++调用(使用指针时)?

Consider a method call in Java when passing references and in c++ when passing pointers. 考虑在传递引用时在Java中使用方法调用,在传递指针时在c ++中考虑使用方法调用。 In the end in both cases I am able to make changes which are visible to the caller. 最后,在两种情况下,我都可以进行更改,使调用者可以看到这些更改。 Also in both cases I'm passing the address of the object to the function. 同样在两种情况下,我都将对象的地址传递给函数。 In fact I'm copying ie making a call by value in c++ too when passing a pointer don't I? 实际上,我也在通过指针在C ++中通过值进行复制即调用,不是吗?

You can simply verify that circumstance by running the following code: 您可以通过运行以下代码来简单地验证这种情况:

#include <iostream>

void modify (int *i) {
    int a = 5;
    i = &a;
}

int main () {
    int b;
    int *i = &b;
    std::cout << i << std::endl;
    modify(i);
    std::cout << i << std::endl;
    return 0;
}

Which will print two times the same address. 它将打印两次相同的地址。

For me it's just not enough to justify a call by reference with the property that you can do a swap function with it. 对我来说,仅仅通过引用来证明调用是合理的,而您可以使用该属性来执行交换功能。 I would like to know the very heart of what makes call by reference call by reference. 我想知道按引用进行调用的根本原因。

Thank you in advance. 先感谢您。

My question in the end comes down to this. 最后我的问题归结于此。 Why is Java Call By Value and C++ Call By Reference (when using pointers) ? 为什么使用值按Java调用和按引用按C ++调用(使用指针时)?

C++ is really call-by-value, not call-by-reference. C ++实际上是按值调用,而不是按引用调用。 The by-reference behavior you see in C++ is a side effect of the fact that you're passing a pointer: the pointer itself is copied by value. 您在C ++中看到的按引用行为是传递指针的事实的副作用:指针本身是按值复制的。 You can't compare these situations because Java doesn't have pointers. 您无法比较这些情况,因为Java没有指针。

The example you give is inappropriate use of call by reference. 您提供的示例不适当地使用按引用调用。 In the function modify 在功能中modify

void modify (int *i) {
    int a = 5;
    i = &a;
}

You declare a local variable a which is on the stack. 您声明一个在堆栈上的局部变量a When it goes out of scope, its address is meaningless. 当它超出范围时,其地址是没有意义的。 So assigning address of a to i has no effect on what i points to. 所以地址分配给ai有什么没有影响i点。

So what makes call by reference work is you can modify the value of what i points to in the function. 因此,通过引用进行调用的原因是您可以修改函数中i指向的值。 For example: 例如:

void modify (int *i) {
    int a = 5;
    *i = a;
}

Therefore in the main function, you can call it like: 因此,在main函数中,您可以像这样调用它:

int b = 2;
int *i = &b;
std::cout << *i << std::endl;  //This will print 2
modify(i);
std::cout << *i << std::endl;  //This will prints 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM