将 auto_ptr 传递给函数实际上使其成为接收器。为什么?

发布于 2024-11-26 23:40:39 字数 220 浏览 0 评论 0原文

我正在阅读一些有关共享指针的注释。 他们说 STL 使用 auto_ptr 的第一次尝试有以下主要缺点:

  • 它们不能在 STL 容器中使用
  • 复制 auto_ptr 转移所有权
  • 将 auto_ptr 传递给函数有效地使其成为接收器

我理解前两个,但不确定最后一个是什么意思。

有人可以解释一下吗?

谢谢。

I'm reading some notes about shared pointers.
They say the first attempt by STL with the auto_ptr had the following major drawbacks:

  • They can't be used in STL containers
  • Copying the auto_ptr transfers ownership
  • Passing an auto_ptr to a function effectively makes it a sink

I understand the first two, but am unsure what the last one means.

Could someone please explain this.

Thanks.

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

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

发布评论

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

评论(3

人间☆小暴躁 2024-12-03 23:40:39

这是因为一旦将 auto_ptr 复制到变量中,您就放弃了指向新变量的指针的所有权。

当您:

void foo(std::auto_ptr<bar> x);

并且使用 auto_ptr 调用 foo 时,您将为 foo 制作 auto_ptr 的副本'使用。这有效地将所有权转移给foo,因此指针在foo完成后被删除。

这是一个非常令人惊讶的行为,让我最终停止使用 auto_ptr。对于 try 块内的简单 RAII(auto_ptr 的主要用例,如书籍中所述),请使用 boost::scoped_ptr

This is because once you copy the auto_ptr into a variable, you forfeit the ownership of the pointer to the new variable.

When you have:

void foo(std::auto_ptr<bar> x);

and you call foo with an auto_ptr, you make a copy of the auto_ptr for foo's use. This effectively transfers ownership to foo and thus the pointer gets deleted after foo is finished.

This is a really surprising behavior that made me definitively stop using auto_ptr. For simple RAII inside a try block (the primary use case of auto_ptr, as described in books), use boost::scoped_ptr.

给妤﹃绝世温柔 2024-12-03 23:40:39

基本上,auto_ptr 将所有权转移到它所分配到的指针。
当您将 auto_ptr 传递给函数时,指针的所有权将转移到函数参数中的接收指针。该指针的作用域仅限于函数体,因此当函数退出时该指针将被删除。

请阅读有效使用 auto_ptr。赫伯·萨特(Herb Sutter)很好地解释了这一点&权威地。

Basically, auto_ptr transfers ownership to the the pointer to which it gets assigned.
When you pass auto_ptr to a function the ownership of the pointer gets transferred to the receiving pointer in the function argument. The scope of this pointer is till the body of the function only and hence the pointer gets deleted when the function exits.

Read about it in Using auto_ptr Effectively. Herb Sutter explains it nicely & authoritatively.

梦开始←不甜 2024-12-03 23:40:39

数据接收器会吸收你的数据,并取得数据的所有权。

该术语起源于“管道”的概念,其中实体链中的某些特定实体从“源”获取数据,然后将其结果推送到“接收器”。链中的下一个实体执行相同的操作,依此类推。在每个阶段,实体无法再对其传递的数据执行任何操作。

通过类比(和示例),考虑从 std::cin (充当源的流)获取数据,进行一些计算,然后将结果推送到 std::cout (充当水槽的溪流)。一旦你完成了,你就完成了;结果是在以太中,你无法取回它们。

这就是 auto_ptr 的作用:它放弃了数据的所有权,无论您是否愿意。

A data sink is something that sucks up your data, taking ownership of it.

The terminology originates in the notion of "pipelines", where some particular entity in a chain of entities takes its data from a "source", then pushes its result to a "sink". The next entity in the chain does the same thing, and so forth. At each stage, the entity can no longer do anything with the data that it's passed on.

By analogy (and example), consider taking data from std::cin (a stream acting as a source), doing some calculations then pushing the result to std::cout (a stream acting as a sink). Once you're done, you're done; the results are out in the ether and you can't get them back.

That's what auto_ptr does: it gives away ownership of your data, whether you wanted it to or not.

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