English 中文(简体)
指针和手法的区别是什么?
原标题:What is the difference between a Pointer and a Handle

我不能说我有一个强大的C++背景,所以我经常听到手势。我知道什么是指针(这个指针存储了内存位置的地址,比如参考地址),但我不知道这两者之间有什么区别。如果你们能提供,C#中的样本代码会很好。

顺便说一下,我查了这个术语, 但是许多人给出了不同的解释, 所以我想我得到了从SO得到最好的一个。

<强度 > EDIT:

< strong> a quick a headup for the other visiters: A handle is like a conference. 它可以像指针一样指向一个资源。 它可以指向一个内存地址, 像指针一样, 但是Handle是更普通的术语, 这样它更像一个伪指针。 文件是这方面的一个好例子。 文件可以有一个 OS 可以理解并用来查找文件的 ID 。 因此, handle 可以持有这个 ID( 可能是或不是记忆地址), 当我们通过此手势时, OS 可以很容易找到文件 。

欲了解更详细的情况,请参阅下文的答复。

<强>编辑:

这个问题下的所有答案都是令人赞叹和非常解释性的,我很难选择其中的一个答案作为答案。

最佳回答

抱歉, 没有 C# 示例, 但是 :

指针是一个内存地址, 在此情况下指向对象存储在内存中的位置。 这是 C++ 从 C 继承的低级别概念 。

关于手柄:

“手柄”一词是指使你能够进入另一个物体的任何技术——一个普遍化的假指针。“手柄”一词(有意)含糊不清。

与物体密切相关的一个更重要的术语是对象参考,这是对象的“别名”。

您可以在"https://isocpp.org/wiki/faq/references#overview-refs" rel=“norefererr”上得到相当明确和简明的回答。

问题回答

控点是用于识别受操作影响对象的抽象对象(比特马普、插座、文件、内存等)的标识符: HBITMAP , socket , FILE/code>是控点的示例 -- -- 它们实际上是在操作系统中某个表格或地图中代表索引的整数。

严格地说,指针是记忆中的确切位置。 虽然指针有时被用作把手,但反之(如果曾经)却很少是真实的。

在对象导向环境中,控件被视为低级别实体,用使用指定控件来调用操作的方法在对象中包装控件是一种良好做法。在这种情况下,您可以参考一个持有控件的物体,例如:CBitmap ,System.Net.Sockets.Socket ,std::fstream 等。

有些人会认为其中之一更干净、更安全、更快、更简单。这几乎总是取决于你所处的环境。如果你有选择的话,你不应该直接在C#中使用手柄,而在C中,使用手柄会更简单(和必要)。

< 强度 > 进口注释 < / 强度 > : 在.Net environment 中, 如果您需要重新排列的话, 您将最终读取一些关于实际对象引用被命名为控点的东西。 这是因为它们实际上是在顶罩下而不是指针下操作的。 所以当您在一个对象上调用一种方法时, 编译者实际上是在将一个带有控点的方法调用到一个可以在记忆中自由移动的对象上。 这样可以让垃圾收集器避免记忆破碎。 因此, 使用一个 < code> System.drawing. Bitmap 你最终会用一个指点到一个手 。

<强度 > EDIT:

C++中的示例、标准流/流:

// Read a short line from a file and output it to the console
// 256 bytes max for simplicity purposes

char buffer[256];

// With handle
FILE* file = fopen ("file.txt" , "r");
fgets(buffer, 256 , file);
std::cout << buffer << std::endl;
fclose (file);

// With object reference
{
  std::ifstream stream ("file.txt");
  stream.getline(buffer, 256);
  std::cout << buffer << std::endl;
}

顶级示例对文件使用“ 控点” 。 < code> file 控点使用 < code> fopen 分配, 并传递给操作, 如 < code> fgets () 和 < code> close () 。 < code> close () 处理控点 。

底示例使用 < code>std:: Ifstream 。 控件在对象构造器中分配, 并且是该对象的内部。 要在文件中操作, 您需要使用该对象提供的方法, 如 < code> gigline () 。 当天体超出范围时, 即关闭括号, 或者如果天体被分配在堆积上, 您需要明确删除它 。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签