gpt4 book ai didi

java - C++ 中传递的函数参数与 Java 中传递的函数参数

转载 作者:行者123 更新时间:2023-12-02 09:27:22 25 4
gpt4 key购买 nike

如果您能给我答案,我会很高兴。我知道Java只是按值传递,而C++是按值传递,但也是按引用传递。我将在下面发布一些代码,以便您了解我的困惑。

在 C++ 中按值传递:

void modify(int x, int y){
x=10;
y=20;
}
void main(){
int a=5,b=8;
cout << a; //outputs 5
cout << b; //outputs 8
modify(a,b);
cout << a; //still outputs 5
cout << b; //still outputs 8
}

modify(a,b); -> 这些参数称为实际参数(传递给函数的参数),来自voidmodify(int x , int y) -> 这些参数称为形式参数(函数接收的参数)。

所有 4 个参数:a、b 和 x、y 都有不同的存储位置。当到达 modify(a,b); 行时,形式参数(x 和 y)将具有实际参数(a 和 b)值的拷贝。换句话说,x将是 5,y 将是 8,但到达这些行:

x=10;y=20;

形式参数的值将分别更改为 10 和 20。之后,修改函数将从堆栈中删除,回到主方法,其中实际参数(a和b)仍然具有相同的值:a=5和b=8。

这是按值传递/调用,它也适用于 Java。

现在,关于通过引用传递/调用(仅在 C++ 中):

void modify(int* p) {
*p = 10;
}
void main()
{
int a = 5;
int* p = &a;
modify(p);
cout << a << endl; //a is now 10.
}

据我目前所知,这是通过引用调用,并且可以使用指针来完成。到达modify(p)行;我们将“a”的地址作为参数传递,该地址由“p”存储,因此在本例中,通过将引用作为函数 voidmodify(int* p) 中的参数,我们在该内存位置访问“a”并到达此行:*p=10;(取消引用)我们将“a”的值设置为 10。

我的问题是:为什么 Java 不被视为按引用传递,因为我可以看到它的行为与 C++ 的情况相同?我已经看到下面的代码发布在另一个有关 Java 的问题上,我还将发布那个人的答案,这让我更加困惑。

public static void main(String[] args){
Dog myDog = new Dog("Rover"); //myDog is a reference, a pointer to the object in memory
foo(myDog); //you're passing the address of the object
}

//Suppose the Dog object resides at memory address 42. This means we pass 42 to the method.

public void foo(Dog someDog) {
someDog.setName("Max"); // AAA
someDog = new Dog("Fifi"); // BBB
someDog.setName("Rowlf"); // CCC
}

/*Let's look at what's happening.
the parameter someDog is set to the value 42
at line "AAA"
someDog is followed to the Dog it points to (the Dog object at address 42)
that Dog (the one at address 42) is asked to change his name to Max
at line "BBB"
a new Dog is created. Let's say he's at address 74
we assign the parameter someDog to 74
at line "CCC"
someDog is followed to the Dog it points to (the Dog object at address 74)
that Dog (the one at address 74) is asked to change his name to Rowlf
then, we return

Did myDog change?
There's the key.
Keeping in mind that myDog is a pointer, and not an actual Dog, the answer is NO. myDog still has the value 42; it's still pointing to the original Dog (but note that because of line "AAA", its name is now "Max" - still the same Dog; myDog's value has not changed.)
If Java had pass-by-reference semantics, the foo method we defined above would have changed where myDog was pointing when it assigned someDog on line BBB.*/

据我所知,在 C++ 中,我们将“a”的值从 5 更改为 10,在 Java 中,通过设置狗的名字从RoverMax,不也是通过引用调用吗?

通过查看此声明:

/*If Java had pass-by-reference semantics, the foo method we defined above would have changed where myDog was pointing when it assigned someDog on line BBB.*/

基于前面的示例,我也在 C++ 中应用了此方法:

void modify(int* p) {
*p = 10;
int b = 20;
p = &b;
}
void main()
{
int a = 5;
int* p = &a;
cout << p << endl; //OUTPUTS memory address 2002(e.g. let's say)
modify(p);
cout << a << endl; //a is now 10.
cout << p << endl; // ALSO OUTPUTS the same memory address 2002
}

但是退出modify函数时,“p”并不指向“b”的地址。

这是我的困惑,因为基于行如果 Java 有传递引用语义,我们上面定义的 foo 方法将会改变 myDog 在 BBB 行上分配 someDog 时指向的位置。在C++中同样如此,结果JAVA和C++都没有按引用传递,而只有按值传递。如果你能告诉我为什么 Java 没有通过引用传递,而它的行为就像我所看到的 C++ 一样(例如 DOG),我将不胜感激!

最佳答案

谈论“pass-by-whatever”时最大的问题是不同的人对这些术语有不同的定义。因此,出于本答案的目的,我将首先给出按值传递和按引用传递的定义。如果对函数内部参数的直接赋值不会影响调用范围内传递的变量,则函数参数是按值传递的。如果直接赋值给函数内部的参数与直接赋值给调用范围中传递的变量具有相同的效果,则函数参数是按引用传递的。

根据这个定义,Java(以及许多其他语言,如 C、JavaScript、Python、Ruby、Scheme)始终是按值传递的。在此定义下,当函数参数具有 & 时,C++ 始终是按值传递。 (包括您在此处放置的两个 C++ 示例),并且当函数参数没有 & 时始终是按值传递。 。您的第二个 C++ 示例是按值传递,因为如果您分配给 p直接(即 p = something; )在 modify() 内,它对调用范围没有影响。在 modify() ,您从未分配给 p直接地;相反,您取消引用 p使用*运算符,并分配给其结果。这与分配给 p 不同。 .

C++ 和 Java 之间最大的区别在于可用的类型。 Java 中唯一的类型是基元类型和引用类型。引用类型的值是“引用”,即指向对象的指针。不存在“对象类型”,因此变量的值不能直接是对象。没有指向原语的指针,或指向对象的指针,或其他指针组合。

您的 Java 示例:

public static void main(String[] args) {
Dog myDog = new Dog("Rover");
foo(myDog);
// here, "myDog" points to the first Dog object, and has the name "Max"
}

public static void foo(Dog someDog) {
someDog.setName("Max");
someDog = new Dog("Fifi");
someDog.setName("Rowlf");
}

相当于此 C++ 代码(忽略内存管理方面):

void main(void) {
Dog *myDog = new Dog("Rover");
foo(myDog);
// here, "myDog" points to the first Dog object, and has the name "Max"
}

void foo(Dog *someDog) {
someDog->setName("Max");
someDog = new Dog("Fifi");
someDog->setName("Rowlf");
}

这两个示例都是按值传递的,因为它们都直接分配给参数 ( someDog = ),并且在这两种情况下,它们对调用范围没有影响 - 它们都只是更改函数内部的指针指向不同的对象,但是它们并不影响main中的指针指着。 (请注意,语法略有不同:Java 中的 Dog 类型相当于 C++ 中的 Dog * 类型,而 C++ 中的 -> 相当于 Java 中的 .。)

C++ 具有对象类型,其中变量的值可以是对象本身。在这种情况下,这种类型的参数意味着在传递时复制对象:

void main(void) {
Dog myDog("Rover");
foo(myDog);
// here, the Dog object in the local variable "myDog" has the name "Fifi"
}

void foo(Dog someDog) {
// this calls the object's assignment operator, which by default
// copies all the fields
someDog = Dog("Fifi");
}

Java 中没有与此等效的东西,因为 Java 没有对象类型。对象始终通过引用(指向对象的指针)进行操作。

您还可以在 C++ 中通过引用传递对象,如下所示:

void main(void) {
Dog myDog("Rover");
foo(myDog);
// here, the Dog object in the local variable "myDog" has the name "Fifi"
}

void foo(Dog &someDog) {
// this calls the object's assignment operator, which by default
// copies all the fields
someDog = Dog("Fifi");
}

这里,对参数 ( someDog = ) 的直接赋值与调用作用域中的相同赋值具有相同的效果。 Java 没有等效项,因为它既没有对象类型,也没有引用传递。

您还可以通过引用传递指向对象的指针:

void main(void) {
Dog *myDog = new Dog("Rover");
foo(myDog);
// here, "myDog" points to the second Dog object, and has the name "Fifi"
// the first Dog object still has the name "Rover"
}

void foo(Dog *&someDog) {
someDog = new Dog("Fifi");
}

这里,对参数 ( someDog = ) 的直接赋值与调用作用域中的相同赋值具有相同的效果,这会导致指针指向不同的 Dog目的。 Java 没有等效项,因为它没有引用传递。然而,有一些语言与 Java 具有类似的类型,但具有引用传递。例如,如果参数标记为 ref,C# 就会按引用传递。或out ,如果参数被标记为 &,PHP 就会按引用传递。 。下面是 C# 中的等效代码:

static void Main() 
{
Dog myDog = new Dog("Rover");
foo(ref myDog);
// here, "myDog" points to the second Dog object, and has the name "Fifi"
// the first Dog object still has the name "Rover"
}
static void foo(ref Dog someDog)
{
someDog = new Dog("Fifi");
}

关于java - C++ 中传递的函数参数与 Java 中传递的函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58250176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com