0

我不确定这个主题是否与 Call-By-Reference 有关,但我对这两个片段有疑问:

爪哇

public static int calculateWinner(int[][] game){
        game[0][0] = 5; 
.
.
.
}


public static void main(String[] args) {

        int[][] game = {
            {1, 2, 2},
            {2, 1, 2},
            {1, 1, 1}
        };

        int winner = calculateWinner(game);
.
.
.
}

C++

int calculateWinner(array<array<int, 3>, 3> field) {
    field[0][0] = 5;
.
.
.
}

int main(int argc, const char* argv[]) {

    array<array<int, 3>, 3> field;
    field[0] = { 2, 0, 1 };
    field[1] = { 0, 1, 0 };
    field[2] = { 1, 0, 2 };

    int winner = calculateWinner(field);
.
.
.
}

因此,如果我在 main 方法中打印出数组,为什么在 Java 中我的位置 [0][0] 的数组是 5 而在 C++ 中却不是?我了解到在 C++ 中它只是范围内的副本。究竟有什么区别?

4

2 回答 2

4

如上所述,这里的主要问题是默认情况下,参数作为值而不是引用传递。在 Java 中也是如此,然而,在 java 片段中传递的任何内容都是指向数组的指针,而不是数组本身,这使它看起来像是“按引用传递”,但是,Java 总是按值传递

在 C++ 中,既可以按值传递,也可以按引用传递。您可以传递一个指向对象的指针,该对象是程序内存中的一个部分,其中包含所需值的位置,或者您可以传递对内存中存储该值的特定位置的引用。请注意以下代码片段中指针的内存地址:

#include <iostream>

void foo(int num)
{
    std::cout << "The location of pass-by-value num is: " << &num << std::endl;
}

void foo1(int* num)
{
    std::cout << "The location of num* is: " << &num << " But its value is :" << num << std::endl;
}

void foo2(int& num)
{
    std::cout << "The location of num& is: " << &num << std::endl;
}

int main()
{
    int num;

    std::cout << "The location of num is: " << &num << std::endl;

    foo(num);
    foo1(&num);
    foo2(num);
}

此代码段的输出是:

The location of num is: 0x7ffce8cceccc

The location of pass-by-value num is: 0x7ffce8ccecac

The location of num* is: 0x7ffce8cceca8 But its value is :0x7ffce8cceccc

The location of num& is: 0x7ffce8cceccc

在 foo 中,我们传递整数的,因此,我们得到一个与原始地址不同的地址。

在 foo1 中,我们将指针的值传递给 num。指针有自己的内存位置,它的就是num的内存地址。

在 foo2 中,我们将引用传递给整数 num,它的内存地址正是原来的内存地址,这才是真正的传递引用。

为了编辑这样的对象,您需要按指针或按引用传递它(通常,只要可能,按引用传递比指针更可取)。

于 2020-03-16T17:03:27.520 回答
1

C++有引用传递和复制传递。Java 仅通过复制传递(但是,对象的内部对象指向与原始对象相同的引用)。

所以:

static void nullReference(int a []){ 
   a = null; // the copy of the object points to null
}

static void update(int a []) { 
 a[0]=5; // [0] is from the original reference.

}

static void main (String args[]){ 
int a [] = {1,2,3}; 
nullReference(a); 
print(a == null); // false
update(a); 
print(a[0] == 5); // true
于 2020-03-16T16:39:52.730 回答