【问题标题】:Java vs C++ (Call-By-Reference?)Java vs C++(引用调用?)
【发布时间】:2020-06-27 18:28:57
【问题描述】:

我不确定这个主题是否与 Call-By-Reference 有关,但我对这 2 个 sn-ps 有疑问:

Java

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++ 中它只是范围内的副本。 到底有什么区别?

【问题讨论】:

  • 一个= 是赋值,两个== 是布尔相等测试。 field[0][0] == 5; 不是 field[0][0] = 5;
  • int calculateWinner(array&lt;array&lt;int, 3&gt;, 3&gt; field) 是按值传递。如果你想通过引用传递,你会做int calculateWinner(array&lt;array&lt;int, 3&gt;, 3&gt;&amp; field)。我不清楚你到底在问什么。
  • 听起来您已经知道答案了? "我了解到在 C++ 中它只是范围内的副本"
  • 无论你如何切片,C 及其变体和 Java 都是按值调用的。您可以传递地址、引用和指针这一事实是无关紧要的。
  • 因为您传递的是对它的引用。您正在更改它指向的内容,这很好,但您不能更改引用本身即使在 C 中,您也可以通过指针将地址传递给方法并更改地址指向的内容,但您不能更改指针,除非你传入它的地址。

标签: java c++ pass-by-reference call-by-value


【解决方案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

【讨论】:

    【解决方案2】:

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

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

    #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);
    }
    

    这个sn-p的输出是:

    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中,我们将reference传递给整数num,它的内存地址正是原来的内存地址,这是一个true 引用传递。

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

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      相关资源
      最近更新 更多