C++中的句柄是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1303123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:29:01  来源:igfitidea点击:

What is a handle in C++?

c++handle

提问by Xenoprimate

I have been told that a handle is sort of a pointer, but not, and that it allows you to keep a reference to an object, rather than the object itself. What is a more elaborate explanation?

有人告诉我,句柄是一种指针,但不是,并且它允许您保留对对象的引用,而不是对象本身。更详细的解释是什么?

回答by Matthew Iselin

A handle can be anything from an integer index to a pointer to a resource in kernel space. The idea is that they provide an abstraction of a resource, so you don't need to know much about the resource itself to use it.

句柄可以是从整数索引到指向内核空间中资源的指针的任何内容。这个想法是它们提供了一个资源的抽象,所以你不需要对资源本身有太多了解就可以使用它。

For instance, the HWND in the Win32 API is a handle for a Window. By itself it's useless: you can't glean any information from it. But pass it to the right API functions, and you can perform a wealth of different tricks with it. Internally you can think of the HWND as just an index into the GUI's table of windows (which may not necessarily be how it's implemented, but it makes the magic make sense).

例如,Win32 API 中的 HWND 是窗口的句柄。它本身是无用的:您无法从中收集任何信息。但是将它传递给正确的 API 函数,您就可以使用它执行大量不同的技巧。在内部,您可以将 HWND 视为 GUI 窗口表的索引(这可能不一定是它的实现方式,但它使魔术有意义)。

EDIT: Not 100% certain what specifically you were asking in your question. This is mainly talking about pure C/C++.

编辑:不能 100% 确定您在问题中具体问的是什么。这里主要讲纯C/C++。

回答by jmucchiello

A handle is a pointer or index with no visible type attached to it. Usually you see something like:

句柄是一个指针或索引,没有附加可见类型。通常你会看到类似的东西:

 typedef void* HANDLE;
 HANDLE myHandleToSomething = CreateSomething();

So in your code you just pass HANDLE around as an opaque value.

因此,在您的代码中,您只需将 HANDLE 作为不透明值传递。

In the code that uses the object, it casts the pointer to a real structure type and uses it:

在使用对象的代码中,它将指针强制转换为真实的结构类型并使用它:

 int doSomething(HANDLE s, int a, int b) {
     Something* something = reinterpret_cast<Something*>(s);
     return something->doit(a, b);
 }

Or it uses it as an index to an array/vector:

或者它使用它作为数组/向量的索引:

 int doSomething(HANDLE s, int a, int b) {
     int index = (int)s;
     try {
         Something& something = vecSomething[index];
         return something.doit(a, b);
     } catch (boundscheck& e) {
         throw SomethingException(INVALID_HANDLE);
     }
 }

回答by Deltics

A handle isa sort of pointer in that it is typically a way of referencing some entity.

句柄一种指针,因为它通常是引用某个实体的一种方式。

It would be more accurate to say that a pointer is one type of handle, but not all handles are pointers.

更准确地说,指针是一种句柄,但并非所有句柄都是指针。

For example, a handle may also be some index into an in memory table, which corresponds to an entry that itself contains a pointer to some object.

例如,句柄也可能是内存表中的某个索引,它对应于一个条目,该条目本身包含指向某个对象的指针。

The key thing is that when you have a "handle", you neither know nor care how that handle actually ends up identifying the thing that it identifies, all you need to know is that it does.

关键是,当您拥有一个“句柄”时,您既不知道也不关心该句柄实际上如何识别它所识别的事物,您只需要知道它确实如此。

It should also be obvious that there is no single answer to "what exactly is a handle", because handles to different things, even in the same system, may be implemented in different ways "under the hood". But you shouldn't need to be concerned with those differences.

很明显,“句柄到底是什么”没有单一的答案,因为不同事物的句柄,即使在同一个系统中,也可能以不同的方式“在幕后”实现。但是您不必担心这些差异。

回答by Mehrdad Afshari

In C++/CLI, a handle is a pointer to an object located on the GC heap. Creating an object on the (unmanaged) C++ heap is achieved using newand the result of a newexpression is a "normal" pointer. A managed object is allocated on the GC (managed) heap with a gcnewexpression. The result will be a handle. You can't do pointer arithmetic on handles. You don't free handles. The GC will take care of them. Also, the GC is free to relocate objects on the managed heap and update the handles to point to the new locations while the program is running.

在 C++/CLI 中,句柄是指向位于 GC 堆上的对象的指针。在(非托管的)C++ 堆上创建对象是通过使用实现new的,new表达式的结果是一个“普通”指针。托管对象通过gcnew表达式在 GC(托管)堆上分配。结果将是一个句柄。您不能对句柄进行指针运算。你没有释放手柄。GC 会照顾他们。此外,GC 可以自由地重新定位托管堆上的对象,并在程序运行时更新句柄以指向新位置。

回答by Johannes Schaub - litb

This appears in the context of the Handle-Body-Idiom, also called Pimpl idiom. It allows one to keep the ABI (binary interface) of a library the same, by keeping actual data into another class object, which is merely referenced by a pointer held in an "handle" object, consisting of functions that delegate to that class "Body".

这出现在Handle-Body-Idiom的上下文中,也称为 Pimpl idiom。它允许通过将实际数据保存到另一个类对象中来保持库的 ABI(二进制接口)不变,该类对象仅由保存在“句柄”对象中的指针引用,该对象由委托给该类的函数组成“身体”。

It's also useful to enable constant time and exception safe swap of two objects. For this, merely the pointer pointing to the body object has to be swapped.

启用两个对象的常量时间和异常安全交换也很有用。为此,只需交换指向 body 对象的指针。

回答by Kulamani

HANDLE hnd;is the same as void * ptr;

HANDLE hnd;是相同的 void * ptr;

HANDLE is a typedef defined in the winnt.h file in Visual Studio (Windows):

HANDLE 是在 Visual Studio (Windows) 中的 winnt.h 文件中定义的 typedef:

typedef void *HANDLE;

Read more about HANDLE

阅读更多关于手柄

回答by dyasta

A handle is whatever you want it to be.

手柄是您想要的任何东西。

A handle can be a unsigned integer used in some lookup table.

句柄可以是在某些查找表中使用的无符号整数。

A handle can be a pointer to, or into, a larger set of data.

句柄可以是指向或指向更大数据集的指针。

It depends on how the code that uses the handle behaves. That determines the handle type.

这取决于使用句柄的代码的行为方式。这决定了句柄类型。

The reason the term 'handle' is used is what is important. That indicates them as an identification or access type of object. Meaning, to the programmer, they represent a 'key' oraccess to something.

使用术语“句柄”的原因很重要。这将它们指示为对象的标识或访问类型。意思是,对于程序员来说,它们代表了“钥匙”对某物的访问。