两者有什么区别

引用传递的参数 参数通过value?

你能给我举几个例子吗?


当前回答

最简单的方法是在Excel文件中获取。举个例子,有两个数字,5和2分别在A1和B1单元格中,你想在第三个单元格中求出它们的和,假设是A2。

有两种方法可以做到这一点。

通过在单元格中输入= 5 + 2将它们的值传递给单元格A2。在这种情况下,如果单元格A1或B1的值发生变化,则A2中的总和保持不变。 或者通过输入= A1 + B1将单元格A1和B1的“引用”传递给单元格A2。在这种情况下,如果单元格A1或B1的值发生变化,A2中的总和也会发生变化。

其他回答

在理解这两个术语之前,您必须了解以下内容。每一个物体都有两个可以使它被区分的东西。

它的价值。 它的地址。

如果你说employee。name = "John",要知道关于name有两件事。它的值是“John”,它在内存中的位置是一个十六进制数,可能像这样:0x7fd5d258dd00。

根据语言的体系结构或对象的类型(类、结构等),可以传输“John”或0x7fd5d258dd00

传递“John”称为按值传递。

传递0x7fd5d258dd00称为引用传递。任何指向这个内存位置的人都可以访问“John”的值。

有关这方面的更多信息,我建议您阅读有关取消指针引用的内容,以及为什么选择struct(值类型)而不是类(引用类型)。

It's a way how to pass arguments to functions. Passing by reference means the called functions' parameter will be the same as the callers' passed argument (not the value, but the identity - the variable itself). Pass by value means the called functions' parameter will be a copy of the callers' passed argument. The value will be the same, but the identity - the variable - is different. Thus changes to a parameter done by the called function in one case changes the argument passed and in the other case just changes the value of the parameter in the called function (which is only a copy). In a quick hurry:

Java only supports pass by value. Always copies arguments, even though when copying a reference to an object, the parameter in the called function will point to the same object and changes to that object will be see in the caller. Since this can be confusing, here is what Jon Skeet has to say about this. C# supports pass by value and pass by reference (keyword ref used at caller and called function). Jon Skeet also has a nice explanation of this here. C++ supports pass by value and pass by reference (reference parameter type used at called function). You will find an explanation of this below.

代码

因为我的语言是c++,我将在这里使用它

// passes a pointer (called reference in java) to an integer
void call_by_value(int *p) { // :1
    p = NULL;
}

// passes an integer
void call_by_value(int p) { // :2
    p = 42;
}

// passes an integer by reference
void call_by_reference(int & p) { // :3
    p = 42;
}

// this is the java style of passing references. NULL is called "null" there.
void call_by_value_special(int *p) { // :4
    *p = 10; // changes what p points to ("what p references" in java)
    // only changes the value of the parameter, but *not* of 
    // the argument passed by the caller. thus it's pass-by-value:
    p = NULL;
}

int main() {
    int value = 10;
    int * pointer = &value;

    call_by_value(pointer); // :1
    assert(pointer == &value); // pointer was copied

    call_by_value(value); // :2
    assert(value == 10); // value was copied

    call_by_reference(value); // :3
    assert(value == 42); // value was passed by reference

    call_by_value_special(pointer); // :4
    // pointer was copied but what pointer references was changed.
    assert(value == 10 && pointer == &value);
}

Java中的一个例子不会有什么坏处:

class Example {
    int value = 0;

    // similar to :4 case in the c++ example
    static void accept_reference(Example e) { // :1
        e.value++; // will change the referenced object
        e = null; // will only change the parameter
    }

    // similar to the :2 case in the c++ example
    static void accept_primitive(int v) { // :2
        v++; // will only change the parameter
    }        

    public static void main(String... args) {
        int value = 0;
        Example ref = new Example(); // reference

        // note what we pass is the reference, not the object. we can't 
        // pass objects. The reference is copied (pass-by-value).
        accept_reference(ref); // :1
        assert ref != null && ref.value == 1;

        // the primitive int variable is copied
        accept_primitive(value); // :2
        assert value == 0;
    }
}

维基百科

http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_value

http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_reference

这家伙说得很准:

http://javadude.com/articles/passbyvalue.htm

当通过引用传递时,基本上是传递一个指向变量的指针。通过值传递,即传递变量的副本。

在基本用法中,这通常意味着通过引用传递,对变量的更改将在调用方法中看到,而在通过值传递时则不会。

下面是一个例子,演示了通过值-指针值-引用传递之间的区别:

void swap_by_value(int a, int b){
    int temp;

    temp = a;
    a = b;
    b = temp;
}   
void swap_by_pointer(int *a, int *b){
    int temp;

    temp = *a;
    *a = *b;
    *b = temp;
}    
void swap_by_reference(int &a, int &b){
    int temp;

    temp = a;
    a = b;
    b = temp;
}

int main(void){
    int arg1 = 1, arg2 = 2;

    swap_by_value(arg1, arg2);
    cout << arg1 << " " << arg2 << endl;    //prints 1 2

    swap_by_pointer(&arg1, &arg2);
    cout << arg1 << " " << arg2 << endl;    //prints 2 1

    arg1 = 1;                               //reset values
    arg2 = 2;
    swap_by_reference(arg1, arg2);
    cout << arg1 << " " << arg2 << endl;    //prints 2 1
}

“参照传递”方法有一个重要的局限性。如果参数声明为引用传递(因此前面有&号),其对应的实际参数必须是变量。

引用“按值传递”形式参数的实际参数一般可以是表达式,因此它不仅可以使用变量,还可以使用文字甚至函数调用的结果。

该函数不能将值放在变量以外的东西中。它不能将新值赋给文字或强制表达式更改其结果。

PS:你也可以在当前的线程中检查Dylan Beattie的答案,它用简单的语言解释了它。

简而言之,通过值传递的是它是什么,通过引用传递的是它在哪里。

如果你的值是VAR1 = "Happy Guy!",你只会看到"Happy Guy!"如果VAR1变成了“Happy Gal!”,你就不会知道了。如果它是通过引用传递的,并且VAR1发生了变化,那么您就会这样做。