什么是指针和引用的真正区别? [英] What is the real difference between Pointers and References?

查看:152
本文介绍了什么是指针和引用的真正区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AKA - ?这是什么痴迷指针

AKA - What's this obsession with pointers?

由于只有真正使用现代,面向对象的语言,如动作脚本,Java和C#,我真的不明白的指针将其用于什么的重要性。那我就在这里错过了?

Having only really used modern, object oriented languages like ActionScript, Java and C#, I don't really understand the importance of pointers and what you use them for. What am I missing out on here?

推荐答案

这一切都只是间接:不处理数据,但说:我会引导您到一些数据,那边的能力。你必须在Java和C#的相同的概念,但仅在参考格式

It's all just indirection: The ability to not deal with data, but say "I'll direct you to some data, over there". You have the same concept in Java and C#, but only in reference format.

主要的区别是引用是不可变的有效路标 - 他们始终指向的东西。这是有用的,易于理解,但比C指针模式不够灵活。 C指针路标,你可以愉快地重写。你知道你要找的字符串被指指隔壁的字符串?好了,只要稍微改变路标。

The key differences are that references are effectively immutable signposts - they always point to something. This is useful, and easy to understand, but less flexible than the C pointer model. C pointers are signposts that you can happily rewrite. You know that the string you're looking for is next door to the string being pointed at? Well, just slightly alter the signpost.

这夫妇以及用C的太露骨了,所需的低层次的知识的做法。我们的知道的一个的char * foo的由一组的位置开始的字符的指向foo的路标。如果我们也知道该字符串是至少10个字符长,我们可以通过改变路标(富+ 5)来指向那么相同的字符串,而是开始长度的一半英寸

This couples well with C's "close to the bone, low level knowledge required" approach. We know that a char* foo consists of a set of characters beginning at the location pointed to by the foo signpost. If we also know that the string is at least 10 characters long, we can change the signpost to (foo + 5) to point at then same string, but start half the length in.

当你知道自己在做什么,死亡,如果你没有这种灵活性是有益的(这里的知不仅仅是知道的语言,它的知道程序的确切状态以上)。弄错了,和你的路标是指导你悬崖的边缘。引用不让你乱动,让你更有信心,你可以跟着他们没有风险(尤其是像A引用的对象永远不会消失的规则加上,因为在大多数垃圾回收的语言)。

This flexibility is useful when you know what you're doing, and death if you don't (where "know" is more than just "know the language", it's "know the exact state of the program"). Get it wrong, and your signpost is directing you off the edge of a cliff. References don't let you fiddle, so you're much more confident that you can follow them without risk (especially when coupled with rules like "A referenced object will never disappear", as in most Garbage collected languages).

这篇关于什么是指针和引用的真正区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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