处理 c++ 中的对象

发布于 2024-11-14 15:04:13 字数 115 浏览 4 评论 0原文

有人告诉我,句柄是一种“空”指针。但“空指针”到底是什么意思以及它的目的是什么。另外,"somehandle = GetStdHandle(STD_INPUT_HANDLE); 是做什么的?

I've been told that a handle is a sort of "void" pointer. But what exactly does "void pointer" mean and what is its purpose. Also, what does "somehandle = GetStdHandle(STD_INPUT_HANDLE); do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩心态 2024-11-21 15:04:13

一般意义上的句柄是唯一标识对象的不透明值。在这种情况下,“不透明”意味着分发句柄的实体(例如窗口管理器)知道句柄如何映射到对象,但使用句柄的实体(例如您的代码)不知道。

这样做是为了除非提供者参与,否则他们无法获取真实的对象,这使得提供者可以确保没有人在背后弄乱其拥有的对象。

因为它非常实用,所以句柄传统上是整数类型或 void*,因为在 C 中使用原语比其他任何东西都容易得多。特别是,Win32 API 中的许多函数接受或返回句柄(使用各种名称进行#define定义:HANDLEHKEY、还有很多其他的)。所有这些类型都映射到 void*

更新:

要回答第二个问题(尽管单独提问和回答可能会更好):

GetStdHandle(STD_INPUT_HANDLE) 返回标准输入的句柄设备。您可以使用此句柄读取进程的标准输入。

A handle in the general sense is an opaque value that uniquely identifies an object. In this context, "opaque" means that the entity distributing the handle (e.g. the window manager) knows how handles map to objects but the entities which use the handle (e.g. your code) do not.

This is done so that they cannot get at the real object unless the provider is involved, which allows the provider to be sure that noone is messing with the objects it owns behind its back.

Since it's very practical, handles have traditionally been integer types or void* because using primitives is much easier in C than anything else. In particular, a lot of functions in the Win32 API accept or return handles (which are #defined with various names: HANDLE, HKEY, many others). All of these types map to void*.

Update:

To answer the second question (although it might be better asked and answered on its own):

GetStdHandle(STD_INPUT_HANDLE) returns a handle to the standard input device. You can use this handle to read from your process's standard input.

做个ˇ局外人 2024-11-21 15:04:13

HANDLE 不一定是指针或双指针,它可能是操作系统表中的索引以及其他任何内容。为了方便起见,将其定义为 void *,因为经常实际上用作指针,并且因为在 C 中,void * 是您可以在其上使用的类型。几乎无法执行任何操作。

关键点是,您必须将其视为某种不透明的令牌,代表操作系统管理的资源;将其传递给适当的函数,告诉它们对此类对象进行操作。因为它是“不透明的”,所以您不应该更改它或尝试取消引用它:只需将它与可以使用它的函数一起使用即可。

A HANDLE isn't necessarily a pointer or a double pointer, it may be an index in an OS table as well as anything else. It's defined for convenience as a void * because often is used actually as a pointer, and because in C void * is a type on which you can't perform almost any operation.

The key point is that you must think at it as some opaque token that represents a resource managed by the OS; passing it to the appropriate functions you tell them to operate on such object. Because it's "opaque", you shouldn't change it or try to dereference it: just use it with functions that can work with it.

夜声 2024-11-21 15:04:13

HANDLE 是一个指向指针的指针,就这么简单。

因此,要获取指向数据的指针,您必须首先取消引用它。

GetStdHandle(STD_INPUT_HANDLE) 将返回 stdin 流 - 标准输入的句柄。如果您使用“<”从命令提示符调用,那么它要么是控制台,要么是文件/流特点。

A HANDLE is a pointer to a pointer, it's pretty much as simple as that.

So to get the pointer to the data, you'd have to dereference it first.

GetStdHandle(STD_INPUT_HANDLE) will return the handle to the stdin stream - standard input. That's either the console or a file/stream if you invoke from the command prompt with a '<' character.

我早已燃尽 2024-11-21 15:04:13

Windows HANDLE 实际上是一个 void 指针数组的索引,加上一些其他东西。 void 指针 (void*) 是指向未知类型的指针,在 C++ 中应不惜一切代价避免使用 - 然而 Windows API 与 C 兼容,并使用它来避免暴露 Windows内部类型。

GetStdHandle(STD_INPUT_HANDLE) 表示获取与标准输出流关联的HANDLE

A Windows HANDLE is effectively an index into an array of void pointers, plus a few other things. A void pointer (void*) is the pointer that points to an unknown type and should be avoided at all costs in C++- however the Windows API is C-compatible and uses it to avoid having to expose Windows internal types.

GetStdHandle(STD_INPUT_HANDLE) means, get the HANDLE associated to the Standard output stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文