如果您能给我一个答案,我会很高兴。我知道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);->参数称为实际参数(传递给函数的参数),并来自void Modify(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。之后,修改功能将从堆栈中删除,返回到main方法,其中实际参数(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”存储,因此,在这种情况下,通过在函数void Modify(int * p)中将引用作为参数,我们访问了“ a” ”,然后到达以下行:* p = 10;(取消引用),我们将“ a”的值设置为10。我的问题是:为什么不也将Java视为按引用调用
通过引用传递
,因为我可以看到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 ++情况来看,在Java中,我们通过将狗的名称从更改为10(在Java中)。流动站到Max,不是通过参考呼叫吗?通过查看此语句:设置为5
/*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
}
但是“ p”退出函数时不指向“ b”的地址。 这是我的困惑,因为基于第modify
如果Java具有传递引用语义,那么我们在上面定义的foo方法将改变myDog在BBB行上分配someDog所指向的位置。
并应用相同的方法在C ++中,结果是JAVA或C ++都没有通过引用传递,而只有通过值传递。如果您能告诉我为什么Java在像C ++那样运行时为什么没有pass by reference(例如,带有DOG的示例),我将不胜感激!如果您能给我一个答案,我会很高兴。我知道Java仅按值传递,而C ++按值传递,也按引用传递。我将在下面发布一些代码,以便您了解我的...