本文介绍了在C ++中处理对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我句柄是一种空"指针.但是空指针"到底是什么意思,其目的是什么.另外,"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?

推荐答案

从广义上讲,句柄是一个唯一标识对象的不透明值.在这种情况下,不透明"表示分配句柄的实体(例如窗口管理器)知道句柄如何映射到对象,但是使用该句柄的实体(例如您的代码)则不知道.

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.

因为它非常实用,所以句柄传统上是整数类型或void*,因为在C语言中使用原语比其他方法都容易得多.特别是Win32 API中的许多函数接受或返回句柄(#define d具有各种名称:HANDLEHKEY,还有许多其他名称).所有这些类型都映射到void*.

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*.

更新:

回答第二个问题(尽管最好单独提出并回答):

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

GetStdHandle(STD_INPUT_HANDLE) 将句柄返回给标准输入设备.您可以使用此句柄来读取流程的标准输入.

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.

这篇关于在C ++中处理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:51