boost shared_ptr:operator=和reset之间的区别?
下面两段代码有什么区别吗?他们中的任何一个比另一个更可取吗?
operator=
boost::shared_ptr<Blah> foo; // foo.ptr should be NULL
foo = boost::shared_ptr<Blah>(new Blah()); // Involves creation and copy of a shared_ptr?
reset
boost::shared_ptr<Blah> foo; // foo.ptr should be NULL
foo.reset(new Blah()); // foo.ptr should point now to a new Blah object
注意:我需要定义shared_ptr,然后将其设置在不同的行中,因为我在一段代码中使用它喜欢:
boost::shared_ptr<Blah> foo;
try
{
foo.reset...
}
foo...
Are there any differences between the two pieces of code below? Is any of them preferable to the other?
operator=
boost::shared_ptr<Blah> foo; // foo.ptr should be NULL
foo = boost::shared_ptr<Blah>(new Blah()); // Involves creation and copy of a shared_ptr?
reset
boost::shared_ptr<Blah> foo; // foo.ptr should be NULL
foo.reset(new Blah()); // foo.ptr should point now to a new Blah object
Note: I need to define the shared_ptr and then set it in a different line because I'm using it in a piece of code like:
boost::shared_ptr<Blah> foo;
try
{
foo.reset...
}
foo...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
operator=
将shared_ptr
分配给shared_ptr
,而reset
使shared_ptr
接受指针的所有权。因此,您发布的示例基本上没有区别。也就是说,您应该不喜欢它们,而只使用make_shared
:此外,如果可能的话,您可以通过将 try-catch 块包装在一个仅返回
shared_ptr
的单独函数中来避免在没有初始化的情况下声明shared_ptr
到新创建的对象:operator=
assigns ashared_ptr
to ashared_ptr
, whilereset
makes ashared_ptr
take ownership of a pointer. So, basically there is no difference between the examples you have posted. That said, you should prefer neither of them and just usemake_shared
:Also, if possible, you can prevent having to declare a
shared_ptr
without initialization by wrapping the try-catch block in a separate function that simply returns ashared_ptr
to the newly created object:operator=
接受另一个shared_ptr
作为参数,从而创建另一个副本(并增加引用计数),而reset()
接受一个指针和一个可选的删除器,因此实际上在当前共享指针之上创建了一个新的共享指针。reset 相当于(并且可能实现为)
operator=
可能实现为:这两个函数的相似之处在于,它们释放对它们已包含的内容(如果有)的控制,并管理不同的内容。指针代替。
operator=
takes anothershared_ptr
as a parameter thus creating another copy (and upping the reference count) whilereset()
takes a pointer and optionally a deleter, thus in reality creating a new shared_ptr on top of the current one.reset is equivalent to (and probably implemented as)
operator=
is likely to be implemented as:The two functions are similar in that they release control of what they are already containing, if any, and manage a different pointer instead.
foo.reset(p)
被定义为等同于shared_ptr(p).swap(foo)
。分配在逻辑上等同于复制和交换,并且可能以这种方式实现。因此 foo = shared_ptr(p); 等价于 foo.swap(shared_ptr(p)) 。如果编译器遇到了非常糟糕的一天,可能会有一个额外的副本。
因此,在您给出的示例中,我认为它们之间没有太多选择。可能还有其他重要的情况。但是 Reset 与模板构造函数一样,对
p
的静态类型进行基于模板的捕获,因此就获取正确的删除器而言,您已经可以了。赋值的主要用途是当您想要复制先前存在的
shared_ptr
时,以共享同一对象的所有权。当然,从临时分配时它也可以正常工作,并且如果您查看不同的重置重载,它们会镜像不同的构造函数。所以我怀疑无论哪种方式你都可以实现同样的目标。foo.reset(p)
is defined to be equivalent toshared_ptr(p).swap(foo)
.Assignment is logically equivalent to copy-and-swap, and possibly implemented that way. So
foo = shared_ptr(p);
is equivalent tofoo.swap(shared_ptr(p))
. Possibly with an extra copy in there if the compiler is having a very bad day.So in the examples you give, I don't think there's much to choose between them. There might be other cases where it matters. But reset does the same template-based capture of the static type of
p
that the template constructor does, so as far as getting the right deleter is concerned, you're covered.The main use of assignment is when you want to copy a previously-existing
shared_ptr
, to share ownership of the same object. Of course it works fine when assigning from a temporary too, and if you look at the differentreset
overloads they mirror the different constructors. So I suspect you can achieve the same things either way.赋值运算符从现有共享对象创建一个新的共享对象,增加引用计数,
而重置调用不会创建新的共享对象,而是创建新的所有权 - 附加到新的底层指针对象(通过控制对象)
Assignment operator create a new shared object from existing one, incrementing the reference count
while the reset call doesn't create the new shared object, but rather a new ownership - attaching to the new underlying pointee ( via control object)