auto_ptr 会防止这种情况发生吗?

发布于 2024-07-15 04:52:29 字数 423 浏览 16 评论 0原文

我不太清楚 auto_ptr 在这种情况下是否会帮助我:

class A
{
  A(const B& member)
   : _member(B)
  {};

...
  const B& _member;
};


A generateA() {
   auto_ptr<B> smart(new B());
   A myA(*smart);
   return myA;
}

smart 离开其封闭范围时,myA._member 引用是否有效? 如果 auto_ptr 不是这里的答案,那么什么是呢?

编辑:我明白了我让大家困惑的地方; 我必须在范围之外返回 myA,这就是为什么我关心 _member 在 smart 退出范围后是否有效。

I am not quite clear if auto_ptr will help me in this case:

class A
{
  A(const B& member)
   : _member(B)
  {};

...
  const B& _member;
};


A generateA() {
   auto_ptr<B> smart(new B());
   A myA(*smart);
   return myA;
}

Will the myA._member reference be valid when smart leaves its enclosing scope? If auto_ptr isn't the answer here, what is?

EDIT: I see where I confused everyone; I have to return myA outside the scope, which is why I care about _member being valid after smart exits the scope.

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

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

发布评论

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

评论(3

橪书 2024-07-22 04:52:29

它不会帮助你。 _member 将成为一个悬空句柄。 这是因为 auto_ptr 保证在作用域结束时销毁:不多也不少。

有 2 个可能的答案。

  • 您可以将 _member 的类型设置为 boost::shared_ptr
  • 或者,如果类 B 是可复制单态,并且对象标识不需要保留 ,您可以将 _member 设为一个值,并在其中存储参数的副本。 这是迄今为止最简单的选择,但显然它是相当有限的。

回应您的编辑:这确实是我所说的情况。 通过按值返回 myA,创建一个副本,并且该副本的 _member 引用已经被破坏的本地。 如上所述,shared_ptr 和值语义都解决了这个问题。

It won't help you. _member will become a dangling handle. This is because auto_ptr guarantees destruction at end of scope: no more, and no less.

There are 2 possible answers.

  • You can make _member's type boost::shared_ptr<const B>.
  • Alternatively, if class B is small, copyable, monomorphic, and object identity doesn't need to be preserved, you can make _member a value, and store a copy of the argument there. This is by far the simplest option but obviously it is quite limiting.

In response to your edit: That is indeed the case that I was talking about. By returning myA by value, a copy is created, and the copy's _member refers to the already destructed local. As described, both shared_ptr and value semantics solve this.

寄居人 2024-07-22 04:52:29

auto_ptr 类是普通指针的包装器。 当堆栈展开时,它们负责取消分配(调用 auto_ptr 的析构函数,从而释放所包含的对象)。

请注意,您的 A 对象也是在堆栈上创建的。 当作用域结束时,A 和 auto_ptr 都将被释放。 除此之外,尝试访问 A 对象将会出现编译时错误。

假设 A 对象是在块之外的某个地方创建的,那么你就会遇到一个真正的问题。 由于A对象存储了对B对象的引用,在块作用域之外,该引用将变得无效。

另请注意,在 C++0x 中,auto_ptr 已被弃用。 请改用 unique_ptr。 请查看通用智能指针,它们是C++0x 即将到来。

The auto_ptr class is a wrapper over normal pointers. They take care of de-allocation when the stack is unwound (the destructor of auto_ptr gets called which in turn frees your contained object).

Note well, that your A object is also created on the stack. When the scope ends, both A and the auto_ptr will be deallocated. Beyond this point trying to access the A object will give you a compile-time error.

Assuming the A object was created somewhere outside the block, then you have a real problem. Since, the A object stores a reference to the B object, outside the block-scope, this reference becomes invalid.

Note also that with C++0x, auto_ptr has been deprecated. Use a unique_ptr instead. Do take a look at the General Purpose Smart Pointers that are coming your way in C++0x.

柠檬色的秋千 2024-07-22 04:52:29
{
   auto_ptr<B> smart(new B());
   A myA(*smart);
}

'smart' 中保存的指针和对象 'myA' 都将在此范围结束时被销毁。 但这应该就是您想要的这段代码。

一旦作用域结束,“myA”将首先被销毁(最后声明)。
那么接下来的这个 smart 就会被销毁,它的析构函数会删除这个指针。

由于无法引用“smart”或“myA”,我希望您此时希望删除指针。

或者你可以这样做:

{
    B  myB;
    A  myA(myB);
}
{
   auto_ptr<B> smart(new B());
   A myA(*smart);
}

Both the pointer held in 'smart' and the object 'myA' will be destroyed at the end of this scope. But that should be what you want with this snipit of code.

Once the scope ends 'myA' will be destroyed first (its declared last).
Then following this smart will be destroyed and its destructor will delete the pointer.

Since there is no way to reference 'smart' or 'myA' I would hope that you want the poiner deleted at this point.

Alternatively you could do this:

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