在这种情况下如何有效地使用 intrusive_ptr ?

发布于 2024-12-23 12:40:25 字数 252 浏览 1 评论 0原文

我已经阅读了很多有关 smart ptr 的内容,并决定将 intrusive_ptr 与我自己的实现一起用于引用计数。

这么说,我现在必须面对另一个问题,考虑到weak_ptr不能与auto_ptr类一起使用,如何解决引用循环。

使用应该存储弱引用的原始指针来解决循环是一件好事吗?这样做的后果是,如果强引用被删除,弱引用将不会被通知/归零,但我认为正确构建依赖关系可能是一个不错的选择,但我在这里可能是错的。

有人可以对此发表任何意见吗?

I have read a bunch about smart ptr, and have decided to use intrusive_ptr with my own implementation for reference counting.

Said that, I have to face now another problem, how to solve reference cycles taking into account weak_ptr can't be used with the auto_ptr class.

Will it be a good thing to resolve the cycles using raw pointers where weak references should be stored? The consecuences of this are that if the strong reference is deleted weak references will not be notified/zeroed, but I think that arquitecting correctly the dependencies it could be a good option, but I could be wrong here.

Could anybody give any opinion about this?.

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

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

发布评论

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

评论(1

梦纸 2024-12-30 12:40:25

只需使用shared_ptr。它更容易使用,并且可以与您提到的 weak_ptr 一起使用。也许有一天您会发现需要使用侵入式的情况,但在那之前,请保持简单。

Just use shared_ptr. It's easier to use, and works with weak_ptr which you mentioned. Maybe someday you'll find a case where you want to use intrusive, but until then, keep it simple.

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