之间的区别是:Handle,Pointer和Reference [英] What is the difference between: Handle, Pointer and Reference

查看:74
本文介绍了之间的区别是:Handle,Pointer和Reference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

句柄与指向对象的指针有何不同?为什么我们不能对引用进行引用?

How does a handle differ from a pointer to an object and also why can't we have a reference to a reference?

推荐答案

句柄通常是对对象的不透明引用.句柄的类型与引用的元素无关.例如,考虑由 open()系统调用返回的文件描述符.类型为 int ,但它表示打开文件表中的一个条目.表中存储的实际数据与 open()返回的 int 无关,从而使实现不必维护兼容性(即,可以透明地重构实际表)不会影响用户代码.句柄只能由同一库接口中的函数使用,可以将句柄重新映射回实际对象.

A handle is usually an opaque reference to an object. The type of the handle is unrelated to the element referenced. Consider for example a file descriptor returned by open() system call. The type is int but it represents an entry in the open files table. The actual data stored in the table is unrelated to the int that was returned by open() freeing the implementation from having to maintain compatibility (i.e. the actual table can be refactored transparently without affecting user code. Handles can only be used by functions in the same library interface, that can remap the handle back to the actual object.

指针是内存中的地址和该内存位置中的对象类型的组合.值是地址,指针的类型告诉编译器可以通过该指针执行哪些操作,如何解释内存位置.指针是透明的,因为所引用的对象具有从指针中出现的具体类型.请注意,在某些情况下,指针可以用作句柄( void * 是完全不透明的,指向空接口的指针也一样不透明).

A pointer is the combination of an address in memory and the type of the object that resides in that memory location. The value is the address, the type of the pointer tells the compiler what operations can be performed through that pointer, how to interpret the memory location. Pointers are transparent in that the object referenced has a concrete type that is present from the pointer. Note that in some cases a pointer can serve as a handle (a void* is fully opaque, a pointer to an empty interface is just as opaque).

引用是对象的别名.这就是为什么不能引用一个引用的原因:您可以为一个对象使用多个别名,但是不能使用别名的别名.与指针一样,也要键入引用.在某些情况下,编译器可以将引用实现为在使用时自动取消引用的指针,在其他情况下,编译器可以具有没有实际存储的引用.重要的部分是它们是对象的别名,必须使用对象对其进行初始化,并且在初始化后不能将其重新引用到其他对象.初始化后,对引用的所有使用都是对实际对象的使用.

References are aliases to an object. That is why you cannot have a reference to a reference: you can have multiple aliases for an object, but you cannot have an alias of an alias. As with pointers references are typed. In some circumstances, references can be implemented by the compiler as pointers that are automatically dereferenced on use, in some other cases the compiler can have references that have no actual storage. The important part is that they are aliases to an object, they must be initialized with an object and cannot be reseated to refer to a different object after they are initialized. Once they are initialized, all uses of the reference are uses of the real object.

这篇关于之间的区别是:Handle,Pointer和Reference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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