句柄和智能指针一样吗?

发布于 2024-09-12 16:09:14 字数 605 浏览 12 评论 0原文

我即将完成 Koenig & Moo 的 Accelerated C++ 以及第 13 章和第 15 章14 他们列出了一些 Handle 类的想法和实现(简单、共享、引用计数)。

这些类封装了原始指针,并将动态对象的分配/释放从客户端代码中抽象出来,以避免原始指针的所有危险,并允许用户取消引用它们以访问指向的对象。基本上是一种与原始内存资源交互的“更安全”方式。

这些章节中介绍的类本质上是智能指针的实现吗?智能指针对我来说仍然很新,但据我了解,这些 Handle 类正在执行相同的功能。

  1. 两者之间有区别还是只是同一事物的另一个名称?
  2. 假设它们在功能上是等效的,那么在实践中,像这样的类是否会从头开始编写,而不是使用已经制作的智能指针解决方案?

编辑

我应该补充一点,他们在这些章节中开发的类是模板类,因此它们不会绑定到特定资源,例如它们没有设计特定的 FileHandle 类。

第一个代码片段这里中的代码,7.1,几乎是他们在我提到的章节中得到了什么。

I'm just about done Koenig & Moo's Accelerated C++ and in Chapters 13 & 14 they lay out the idea and implementation of a few Handle classes (simple, shared, reference counted).

The classes encpasulate a raw pointer and abstract the allocation / deallocation of dynamic objects away from the client code to avoid all the dangers of raw pointers as well allowing the user to dereference them to access the pointed to object. Basically a 'safer' way to interface with raw memory resources.

Are the classes presented in these chapters essentially implementations of smart pointers? Smart pointers are still pretty new to me but from what I understand these Handle classes are performing the same function.

  1. Is there a a distinction between the two or is it just another name for the same thing?
  2. Assuming they're equivalent in function, in practice would a class like this ever be written from scratch rather than using an already made smart pointer solution?

EDIT

I should add that the classes they develop in these chapters are template classes so they're not bound to a specific resource, as in they're not designing a specific FileHandle class for example.

The code in the first code snippet here, 7.1, is pretty much what they've got in the chapters I'm referring to.

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

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

发布评论

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

评论(3

网白 2024-09-19 16:09:14

从你的描述来看,它听起来像是一个智能指针。
虽然我个人不会使用句柄这个术语,因为它有点含糊(只需将智能指针称为智能指针)。

问:你能从头开始写一个智能指针吗?
答:是的

问:你是否应该编写自己的智能指针。
答:不。这比你想象的要棘手得多。甚至书中的描述也只是掩盖了更复杂的问题。坚持使用信誉良好的图书馆提供的标准。

问:处理这是什么意思。
答:以前的意思是指向指针的指针。但多年来,随着该术语的重复使用,该术语变得含糊不清。但您可以将其视为指向资源的指针,该资源可以通过其他方法访问,并且在这种情况下,它们的描述是有效的。

问:是否应该使用智能指针而不是原始内存。
答:除非绝对必要(实际上从来没有),否则永远不要使用原始内存,始终将其封装在管理它的类中。该类是智能指针还是其他类型的对象是一个很好的问题。 std::vector 不被视为智能指针,但它封装了 RAW 指针。

问:是否应该从业务逻辑中抽象出内存管理:
答:这是一个棘手的问题,有些人可能会不同意。但我会说永远。一个类应该做一件事。问题要么是资源管理,要么是业务逻辑。将这两个线索结合起来会带来难以预料的复杂性。

From your description it sounds like a smart pointer.
Though personally I would not use the term handle as it is slightly ambiguous (just call a smart pointer a smart pointer).

Q: Could you write a smart pointer from scratch?
A: Yes

Q: Should you write your own smart pointer.
A: No. Its a lot trickier than you think. Even the description in the book only glossed over the more complex problems. Stick with the standard ones that are provided by reputable libraries.

Q: Handles what does it mean.
A: It used to mean a pointer to a pointer. But the term has come ambiguous over the years as the term has been re-used. But you can consider it a pointer to a resource where the resource can be accessed by other methods and in this context their description is sort of valid.

Q: Should you use smart pointers instead of raw memory.
A: Never use RAW memory unless absolutely necessary (which is practically never) always encapsulate it in a class that manages it. Whether the class is a smart pointer or another type of object is a good question. std::vector is not considered a smart pointer but it encapsulates a RAW pointer.

Q: Should you abstract memory management from business logic:
A: That's a tricky one and some would may disagree. But I would say always. A class should do one thing. The thing is either resource management OR business logic. combining the two leads too unforeseen complexities.

天赋异禀 2024-09-19 16:09:14

我想说句柄是比智能指针更普遍的概念。

例如,考虑文件句柄。文件句柄实际上可能是指向表示打开文件的数据结构的智能指针,但客户端并没有真正将其视为指针,而是只是将句柄传递给 API。

所以,是的,我曾多次编写过自己的句柄类。

I would say that a handle is a more general concept than a smart pointer.

Consider, for example, file handles. A file handle may in effect be a smart pointer to a data structure representing an open file, but the client doesn't really treat it a pointer, rather they just pass the handle down to an API.

So, yes I have on several occasions written my own handle classes.

鹊巢 2024-09-19 16:09:14

如果您可以原谅 C++ 的一些隐喻,那么这种关系类似于:class smart_pointer : public handle;。智能指针是一个句柄,它在句柄和它正在处理的任何内容之间提供特定(类似指针)样式的接口。非智能指针的句柄通常具有(至少模糊地)类似的操作,但它们的接口可能完全不同。

在某些时候,显然有人必须编写智能指针类。与此同时,这并不是一件可以轻易承担的事情。除非您有极其精确的规格,否则您可能至少会在一些细节上出错——而“极其精确的规格”只会将几率降低到大约 50:50...

OTOH,它是仍然没有设计智能指针那么糟糕。这往往非常棘手。举一个明显的例子,auto_ptr 在标准化过程中被设计和重新设计了多次,但它在现实生活中的使用仍然相对较少,并且已经在 C++0x 中被弃用(由std::unique_ptr,这是大量工作、辩论以及大量实验和测试的产物。

If you'll forgive a bit of metaphorical C++, the relationship is something like: class smart_pointer : public handle;. A smart pointer is a handle that provides a specific (pointer-like) style of interface between the handle and whatever it's handling. A handle that's not a smart pointer will generally have (at least vaguely) similar operations, but their interface may be entirely different.

At some point, somebody obviously has to write the smart pointer class(es). At the same time, this is not something to undertake lightly. Unless you have an extremely precise specification, you'll probably get at least a few details wrong -- and that "extremely precise specification" only reduces the odds to about 50:50...

OTOH, it's still not nearly as bad as designing a smart pointer. This tends to be tricky in the extreme. For an obvious example, auto_ptr was designed and re-designed a number of times during standardization, but it still sees relatively little use in real life, and is already being deprecated in C++0x (replaced by std::unique_ptr, which is the product of a lot of work, debate, and no small amount of experimentation and testing).

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