将 auto_ptr 传递给函数实际上使其成为接收器。为什么?
我正在阅读一些有关共享指针的注释。 他们说 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为一旦将
auto_ptr
复制到变量中,您就放弃了指向新变量的指针的所有权。当您:
并且使用
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:
and you call
foo
with anauto_ptr
, you make a copy of theauto_ptr
forfoo
's use. This effectively transfers ownership tofoo
and thus the pointer gets deleted afterfoo
is finished.This is a really surprising behavior that made me definitively stop using
auto_ptr
. For simple RAII inside atry
block (the primary use case ofauto_ptr
, as described in books), useboost::scoped_ptr
.基本上,
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.
数据接收器会吸收你的数据,并取得数据的所有权。
该术语起源于“管道”的概念,其中实体链中的某些特定实体从“源”获取数据,然后将其结果推送到“接收器”。链中的下一个实体执行相同的操作,依此类推。在每个阶段,实体无法再对其传递的数据执行任何操作。
通过类比(和示例),考虑从
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 tostd::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.