Java vs C ++通过引用传递 [英] java vs C++ pass by reference

查看:87
本文介绍了Java vs C ++通过引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我对以下内容感到困惑:
在C ++中,我们可以通过引用(将其声明为指针或引用变量)将参数传递给函数,并且如果在函数内部对其进行修改,则当函数返回时,更改将反映给调用方. 这不是在Java中发生的,而且我不确定我为什么会这样.


I am confused in the following:
In C++ we can pass a parameter to a function by reference (having declared it as a pointer or reference variable) and if we modify it inside the function, the changes are reflected to the caller when the function returns.
This is not happening in java and I am not sure I understand why.

例如这是来自对象X的方法

E.g. this is a method from an object X

public boolean aMethod(int id, myClass aClass)
{
   //do some logic
   aClass = new MyClass();
   //configure argument object aClass
   return true;
}

呼叫代码中:

//some code processing
myClass obj = null;
if(X.aMethod(2,obj))
{
  obj.methodA();//start using the object
}

我在C ++中使用它,即返回一个结果,通知该函数参数可以使用,但是在Java中这是行不通的.
我进入if(X.aMethod(2,obj))分支,但obj为空.为什么为空?
我不是使用aMethod(int id,myClass aClass)方法中的new从堆中分配了一个内存地址吗?我没有在函数中传递obj的地址"吗? 我期望在调用代码中正确构造和使用obj.我是否误解了有关Java中内存的内容?

I use it in C++, i.e. to return a result that notifies that the function parameter can be used, but in java this does not work.
I enter the if(X.aMethod(2,obj)) branch but the obj is null. Why is it null?
Haven't I assigned a memory address from the heap using new inside the method aMethod(int id, myClass aClass)? Am I not passing the "address" of obj in the function? I was expecting to have the obj properly constructed and usable in the calling code. Have I misunderstood something concerning memory in java?

推荐答案

Java通过值传递所有内容-包括引用.

Java passes everything by value - including references.

这意味着,如果您传递一个对象,则可以修改该对象的 properties ,并且它们在返回后仍将保留,但是您不能替换整个对象带有一个全新的对象,因为您实际上不能修改 reference -仅可以修改引用指向的对象.

What this means is that if you pass an object, you can modify properties of that object, and they will persist after you return, but you can't replace the object in its entirety with a completely new object, because you can't actually modify the reference - only what the reference points to.

aClass =行之前:

Outside the function:

    obj ---> <foo>

Inside the function:

    aClass ---> <foo>

aClass =行之后:

Inside the function:

    aClass ---> <bar>

Outside the function:

    obj ---> <foo>

这里要注意的关键是aClass并不指向obj-它指向<foo>.您没有传递obj的地址,而是传递了 obj指向的地址.因此,当您更改aClass指向的内容时,不会碰到obj.

The key thing to notice here is that aClass doesn't point to obj - it points to <foo>. You're not passing the address of obj, you're passing the address of what obj points to. Thus, when you change what aClass points to, that doesn't touch obj.

另一种思考方式:

在Java中,

Bar foo = new Bar();

等效于C ++,

Bar *foo = new Bar();

因此,当您将foo传递给函数时,您没有传递foo的地址-您正在传递分配的对象的地址. Java没有C/C ++具备的& -operator样式传递引用.

Thus when you pass foo to a function, you're not passing the address of foo - you're passing the address of the allocated object. Java doesn't have the &-operator style pass-by-reference that C/C++ do.

这篇关于Java vs C ++通过引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆