提振,共享PTR VS弱PTR?它何时使用? [英] boost, shared ptr Vs weak ptr? Which to use when?

查看:132
本文介绍了提振,共享PTR VS弱PTR?它何时使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的项目我使用的boost :: shared_ptr的相当广泛。

In my current project I am using boost::shared_ptr quite extensively.

最近我的同胞队友也使用的weak_ptr 启动。我不知道该用哪一个时。

Recently my fellow team mates have also started using weak_ptr. I don't know which one to use and when.

除了这个,我应该怎样做,如果我想的weak_ptr 转换为的shared_ptr 。难道把一个锁的weak_ptr 来在其他线程创建一个的shared_ptr 影响我的code?

Apart from this, what should I do if I want to convert weak_ptr to shared_ptr. Does putting a lock on weak_ptr to create a shared_ptr affect my code in other thread?

推荐答案

在概括和总结,

强大的指针的保证自己的有效性。使用它们,例如,当

Strong pointers guarantee their own validity. Use them, for example, when:


  • 您自己所指向的对象;你创建它并摧毁它

  • 您不具备,如果对象不存在定义的行为

  • 您需要强制执行的对象存在。

弱指针的保障的知道的自己的有效性。使用它们,例如,当

Weak pointers guarantee knowing their own validity. Use them, for example, when:


  • 您访问它,但它不是你的。

  • 如果该对象不存在您已经定义的行为

锁定()在弱指针返回一个指针强;你这是怎么进入弱指针。如果对象不再有效(它被删除等),则强指针将是NULL,否则,将在对象指向。您需要检查。

Lock() on a weak pointer returns a strong pointer; this is how you access the weak pointer. If the object is no longer valid (it's been deleted, etc), then the strong pointer will be NULL, otherwise, it will point at the object. You will need to check this.

它的这种方式设置,这样当你使用它,你可以不小心删除了对象,因为你做了一个临时的(本地)强大的指针,从而garunteed物体的存在而强大的指针依然存在。当你使用这个对象,通常让强大的指针下跌超出范围(或重新分配它),然后允许对象被删除。对于多线程,对待他们一样小心你如何对待那些不具有内置的线程安全的其他东西,注意我上面提到的担保的将会的持有多线程的时候。据我所知他们没有做什么特别的事情过去那种。

It's set up this way so that you cannot accidentally delete the object while you're using it, because you've made a temporary (local) strong pointer, and thus garunteed the object's existence while that strong pointer remains. When you're done using the object, you generally let the strong pointer fall out of scope (or reassigning it), which then allows the object to be deleted. For multithreading, treat them with same care you treat other things that don't have built-in thread safety, noting that the guarantee I mentioned above will hold when multithreading. AFAIK they don't do anything special past that.

升压共享指针也有垃圾收集类似的特征,当最后一个强烈的指向对象的指针消失或指向其他地方,该对象被删除,因为。

The boost shared pointers also have garbage-collector like features, since when the last strong pointer to an object goes away or points somewhere else, the object gets deleted.

还有在其他的答案中提到的性能和循环依赖。

There's also the performance and circular dependencies mentioned in the other answers.

从根本上说,我会说,升压共享指针库让你不会弄乱组建一个程序,但它是采取正确设计你的指针,对象所有权和寿命的时间内没有替代品。如果你有这样的设计,可以使用库来执行它。如果你没有这样的设计,你可能会遇到不同的问题比以前。

Fundamentally, I would say that the boost shared pointer library allows you to not mess up putting together a program, but it is no substitute for taking the time to properly design your pointers, object ownerships and lifetimes. If you have such a design, you can use the library to enforce it. If you don't have such a design, you're likely to run into different problems than before.

这篇关于提振,共享PTR VS弱PTR?它何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆