问题 - 函数返回类对象(比较C ++和JAVA) [英] question--A function returns class object(comparing C++ & JAVA)

查看:95
本文介绍了问题 - 函数返回类对象(比较C ++和JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,


我对一个返回类对象的函数感到有点困惑,对于

例子,假设我有一个类Money和一个方法:


Money lastYear(Money aMoney)

{

Money tempMoney;

.. 。

返回tempMoney;

}


因为在C ++中,RETURNED实际上是对象的新副本(种类

就像传递值一样),这里返回一个tempMoney的副本,所以

tempMoney将无用,所以在返回语句后,对象

tempMoney将被销毁。这是正确的吗?


现在,如果在JAVA(对不起,没有冒犯),因为总是通过

引用,如果上面的代码是Java,那么返回是对象tempMoney的内存地址

。因此在函数退出后,对象

tempMoney被其他变量引用。所以它仍然存在。这是

是否正确?


非常感谢。

解决方案

Xiaoshen Li写道:

我对一个返回类对象的函数感到有点困惑,例如,假设我有一个类Money和一个方法:

Money lastYear(Money aMoney)
{钱tempMoney;
...
返回tempMoney;
}

因为在C ++中,RETURNED实际上是对象的新副本(类似于传递值的类型),这里返回了tempMoney的副本,所以
tempMoney将无用,所以在返回语句之后,对象
tempMoney将被销毁。它是否正确?


''tempMoney''是一个本地对象。当''lastYear''

函数完成执行时,无论你返回什么(或如何),它都会被销毁。

现在,如果在JAVA中(抱歉,不要冒犯) ,因为总是通过
引用,如果上面的代码是Java,则RETURNED是对象tempMoney的MEMORY ADDRESS。


我会接受你的话。

因此在函数退出后,对象
tempMoney被其他变量引用。所以它仍然存在。这个
是正确的吗?




为什么不在Java新闻组中询问Java内部结构?


V


Victor Bazarov写道:

Xiaoshen Li写道:

我对一个返回的函数有点疑惑一个类对象,例如,假设我有一个类Money和一个方法:

Money lastYear(Money aMoney)
{
Money tempMoney; ...
返回tempMoney;
}

因为在C ++中,RETURNED实际上是对象的新副本(类似于传递值),这里返回了一个tempMoney的副本,所以
tempMoney将无用,所以在返回语句之后,对象的tempMoney将被销毁。这是正确的吗?



''tempMoney''是一个本地对象。无论你返回什么(或如何),'lastYear''
函数完成执行时都会被销毁。




到OP,你''重新确认tempMoney被销毁,但如果通过

无用你的意思是未引用的然后我怀疑你可能有误b / b
误解了原因。

正如Victor指出的那样,tempMoney一旦被销毁就会被销毁

超出范围只是因为它是一个本地对象。它与

无关,是否存在对该对象的引用仍然存在。例如,如果你不明智,你可以写:


//哎呀!返回悬空参考

Money& lastYear(Money aMoney)

{

Money tempMoney;

....

返回tempMoney;

}


在这种情况下,tempMoney仍然被销毁,函数返回的引用

绑定到一个不存在的对象。

与Java不同,引用的存在对引用对象的生命周期没有影响。同样,指针确实不会影响指针的生命周期。这取决于您确定没有悬挂的引用或指针。


ni ***** @ microsoft.com 写道:

[...]
与Java不同,引用的存在对引用对象的生命周期没有影响。


实际上这通常不正确。如果一个临时对象绑定了一个const

引用,那么临时就会持久存在。

但是临时对象的生命周期是在编译时维护的,不是在
运行时间。当然,这是一个特例。你是绝对正确的

WRT指针。

[..]




V


Dear Sir,

I am a little puzzled about a function returning a class object, for
example, suppose I hava a class Money and a method:

Money lastYear(Money aMoney)
{
Money tempMoney;
...
return tempMoney;
}

Because in C++ the RETURNED is actually a new copy of the object(kind of
like passing by value), here a copy of tempMoney is returned, so
tempMoney will be useless, so after the statement of return, the object
tempMoney will be destroyed. Is this correct?

Now, if in JAVA(sorry, not offending), since always passing by
reference, if above code is Java, the RETURNED is the MEMORY ADDRESS of
the object tempMoney. So after the exit of the function, the object
tempMoney is referenced by other variable. So it stays existing. Is this
correct?

Thank you very much.

解决方案

Xiaoshen Li wrote:

I am a little puzzled about a function returning a class object, for
example, suppose I hava a class Money and a method:

Money lastYear(Money aMoney)
{
Money tempMoney;
...
return tempMoney;
}

Because in C++ the RETURNED is actually a new copy of the object(kind of
like passing by value), here a copy of tempMoney is returned, so
tempMoney will be useless, so after the statement of return, the object
tempMoney will be destroyed. Is this correct?
''tempMoney'' is a local object. It will be destroyed when ''lastYear''
function finishes execution no matter what (or how) you return.
Now, if in JAVA(sorry, not offending), since always passing by
reference, if above code is Java, the RETURNED is the MEMORY ADDRESS of
the object tempMoney.
I''ll take your word for it.
So after the exit of the function, the object
tempMoney is referenced by other variable. So it stays existing. Is this
correct?



Why don''t you ask about Java internals in a Java newsgroup?

V


Victor Bazarov wrote:

Xiaoshen Li wrote:

I am a little puzzled about a function returning a class object, for
example, suppose I hava a class Money and a method:

Money lastYear(Money aMoney)
{
Money tempMoney;
...
return tempMoney;
}

Because in C++ the RETURNED is actually a new copy of the object(kind of
like passing by value), here a copy of tempMoney is returned, so
tempMoney will be useless, so after the statement of return, the object
tempMoney will be destroyed. Is this correct?



''tempMoney'' is a local object. It will be destroyed when ''lastYear''
function finishes execution no matter what (or how) you return.



To the OP, you''re correct that tempMoney is destroyed, but if by
"useless" you mean "unreferenced" then I suspect you may have
misunderstood the reason.

As Victor pointed out, tempMoney is destroyed as soon as it
passes out of scope simply because it is a local object. It has
nothing to do with whether any references to the object still
exist. For example, if you were unwise, you could have written:

// oops! returns a dangling reference
Money& lastYear(Money aMoney)
{
Money tempMoney;
....
return tempMoney;
}

In this case, tempMoney is still destroyed and the reference
returned by the function is bound to a non-existent object.
Unlike in Java, the existence of a reference has no impact on
the lifetime of the referenced object. Likewise, a pointer does
not affect the lifetime of the pointee. It''s up to you to make
sure there are no dangling references or pointers.


ni*****@microsoft.com wrote:

[...]
Unlike in Java, the existence of a reference has no impact on
the lifetime of the referenced object.
Actually this is not generally true. If a temporary object has a const
reference bound to it, the temporary persists as long as the reference.
But the lifetime of a temporary is maintained at compile-time, not at
run-time. It''s a special case, of course. And you''re absolutely right
WRT pointers.
[..]



V


这篇关于问题 - 函数返回类对象(比较C ++和JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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