Java参考和C ++参考之间的区别 [英] difference between java reference and c++ reference
本文介绍了Java参考和C ++参考之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
java参考和c ++参考之间有什么区别?
我在google上搜索了此内容,但未找到任何内容!
what is the difference between java reference and c++ reference?
i google this but found nothing!
推荐答案
在C ++中,引用只是对象的别名.它们只能引用初始化引用时设置的单个对象.
Java引用类似于C ++中的std::shared_ptr
.它们一次只能引用一个对象,但是它们可以反弹.它们还为对象的生存期提供了一个下限-只要对该对象有一个引用,该对象就不会被垃圾回收.
总结一下:
-C ++引用在引用的生存期内绑定到同一对象,并且在控制对象的生存期中不起作用.[1]
-Java引用一次绑定到一个对象,但是在程序执行时可以将其分配给其他对象.如果存在对对象的任何引用,则不会进行垃圾回收,这与C ++中的引用不同,C ++中对对象的引用并不意味着该对象还活着.
[1]有一个例外-对临时对象的const引用只要引用还活着就可以使临时对象保持活动状态.
In C++ references are just an alias for an object. They can only ever refer to a single object which is set when the reference is initialised.
Java references are likestd::shared_ptr
in C++. They can only ever refer to one object at a time but they can be rebound. They also give a lower bound for the lifetime of the object - as long as there''s one reference to the object the object won''t be garbage collected.
So to sum up:
- C++ references are bound to the same object for the lifetime of the reference and don''t play any part in controlling the lifetime of the object.[1]
- Java references are bound to one object at a time but can be assigned other objects while the program executes. If any reference exists to an object it won''t be garbage collected, unlike in C++ where having a reference to an object doesn''t mean that the object''s alive.
[1] with one exception - a const reference to a temporary object keeps the temporary object alive for as long as the reference is alive.
请参考类似的讨论:
Java参考与C ++之间的区别是什么参考 [ ^ ]
Please refer similar discussion:
What''s the difference between a Java reference and C++ reference[^]
看这个例子并尝试理解.希望它将解决您的问题.
这篇关于Java参考和C ++参考之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文