auto_ptr 会防止这种情况发生吗?
我不太清楚 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它不会帮助你。 _member 将成为一个悬空句柄。 这是因为
auto_ptr
保证在作用域结束时销毁:不多也不少。有 2 个可能的答案。
boost::shared_ptr
。回应您的编辑:这确实是我所说的情况。 通过按值返回 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.
boost::shared_ptr<const B>
.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.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 ofauto_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 theA
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, theA
object stores a reference to theB
object, outside the block-scope, this reference becomes invalid.Note also that with C++0x,
auto_ptr
has been deprecated. Use aunique_ptr
instead. Do take a look at the General Purpose Smart Pointers that are coming your way in C++0x.'smart' 中保存的指针和对象 'myA' 都将在此范围结束时被销毁。 但这应该就是您想要的这段代码。
一旦作用域结束,“myA”将首先被销毁(最后声明)。
那么接下来的这个 smart 就会被销毁,它的析构函数会删除这个指针。
由于无法引用“smart”或“myA”,我希望您此时希望删除指针。
或者你可以这样做:
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: