Java 与 C++(按引用调用?)
Java vs C++ (Call-By-Reference?)
我不确定这个主题是否与 Call-By-Reference 有关,但我对这两个片段有疑问:
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++ 中它只是范围内的副本。
具体有什么区别?
C++ 有引用传递和复制传递。
Java 只通过copy(但是,对象的内部对象指向与原来相同的引用)。
所以:
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
如上所述,这里的主要问题是默认情况下,参数作为值而不是引用传递。在 Java 中也是如此,但是,在 java 片段中传递的是指向数组的 指针 ,而不是数组本身,因此看起来因为 "pass by reference"、 然而 、Java 总是 按值传递 !
在 C++ 中,可以按值和按引用传递。您可以传递指向对象的指针,它是程序内存中的 new 部分,其中包含所需值的位置,或者您可以传递对特定位置的引用存储值的内存。请注意以下代码片段中指针的内存地址:
#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 中,我们传递了整数的 value,因此,我们得到了与原始地址不同的地址。
在foo1中,我们将指针的值传递给num。指针有自己的内存位置,它的value就是num.
的内存地址
在foo2中,我们将reference传给整数num,它的内存地址正好是原来的内存地址,这是一个 true 传递引用。
为了编辑这样的对象,您需要通过指针或引用传递它(通常,只要有可能,传递引用优于指针)。
我不确定这个主题是否与 Call-By-Reference 有关,但我对这两个片段有疑问:
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++ 中它只是范围内的副本。 具体有什么区别?
C++ 有引用传递和复制传递。 Java 只通过copy(但是,对象的内部对象指向与原来相同的引用)。
所以:
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
如上所述,这里的主要问题是默认情况下,参数作为值而不是引用传递。在 Java 中也是如此,但是,在 java 片段中传递的是指向数组的 指针 ,而不是数组本身,因此看起来因为 "pass by reference"、 然而 、Java 总是 按值传递 !
在 C++ 中,可以按值和按引用传递。您可以传递指向对象的指针,它是程序内存中的 new 部分,其中包含所需值的位置,或者您可以传递对特定位置的引用存储值的内存。请注意以下代码片段中指针的内存地址:
#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 中,我们传递了整数的 value,因此,我们得到了与原始地址不同的地址。
在foo1中,我们将指针的值传递给num。指针有自己的内存位置,它的value就是num.
的内存地址在foo2中,我们将reference传给整数num,它的内存地址正好是原来的内存地址,这是一个 true 传递引用。
为了编辑这样的对象,您需要通过指针或引用传递它(通常,只要有可能,传递引用优于指针)。