我应该使用shared_ptr或unique_ptr [英] Should I use shared_ptr or unique_ptr

查看:163
本文介绍了我应该使用shared_ptr或unique_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用pimpl成语做一些对象,但我不确定是否使用 std :: shared_ptr std :: unique_ptr

I've been making some objects using the pimpl idiom, but I'm not sure whether to use std::shared_ptr or std::unique_ptr.

我明白 std :: unique_ptr 更高效,但这不是一个问题我,因为这些对象是相对重量无论如何,因此 std :: shared_ptr over std :: unique_ptr 的成本是相对

I understand that std::unique_ptr is more efficient, but this isn't so much of an issue for me, as these objects are relatively heavyweight anyway so the cost of std::shared_ptr over std::unique_ptr is relatively minor.

我目前使用 std :: shared_ptr 只是因为额外的灵活性。例如,使用 std :: shared_ptr 允许我将这些对象存储在hashmap中以便快速访问,同时仍然能够将这些对象的副本返回给调用者(我相信任何迭代器或引用都可能很快变得无效。)

I'm currently going with std::shared_ptr just because of the extra flexibility. For example, using a std::shared_ptr allows me to store these objects in a hashmap for quick access while still being able to return copies of these objects to callers (as I believe any iterators or references may quickly become invalid).

然而,这些对象在某种程度上并没有被复制,因为更改影响所有副本,所以我想知道也许使用 std :: shared_ptr 并允许副本是某种反模式或坏事。

However, these objects in a way really aren't being copied, as changes affect all copies, so I was wondering that perhaps using std::shared_ptr and allowing copies is some sort of anti-pattern or bad thing.

正确?

推荐答案


我使用pimpl idiom做了一些对象,不确定是否使用 shared_ptr unique_ptr

绝对 unique_ptr scoped_ptr

Pimpl 不是模式,而是一个处理编译时依赖和二进制兼容性的习语。它不应该影响对象的语义,特别是关于其复制行为。

Pimpl is not a pattern, but an idiom, which deals with compile-time dependency and binary compatibility. It should not affect the semantics of the objects, especially with regard to its copying behavior.

你可以使用你想要的任何类型的智能指针,但那些2保证你不会意外地在两个不同的对象之间共享实现,因为他们需要有意识的决定关于复制构造函数和赋值运算符的实现。

You may use whatever kind of smart pointer you want under the hood, but those 2 guarantee that you won't accidentally share the implementation between two distinct objects, as they require a conscious decision about the implementation of the copy constructor and assignment operator.


然而,这些对象在某种程度上真的不被复制,因为更改影响所有副本,所以我想知道可能使用 shared_ptr 并允许副本是一些

这不是一个反模式,事实上,它是一个模式:别名。你已经使用它,在C + +,裸指针和引用。 shared_ptr 提供额外的安全性措施,以避免死锁引用,以额外的复杂性和新问题为代价(注意创建内存泄漏的周期)。

It is not an anti-pattern, in fact, it is a pattern: Aliasing. You already use it, in C++, with bare pointers and references. shared_ptr offer an extra measure of "safety" to avoid dead references, at the cost of extra complexity and new issues (beware of cycles which create memory leaks).

与Pimpl无关

Unrelated to Pimpl


我知道 unique_ptr 是更有效率,但这不是一个问题,对我来说,因为这些对象是相对重量无论如何,所以成本 shared_ptr over unique_ptr 相对较小。

I understand unique_ptr is more efficient, but this isn't so much of an issue for me, as these objects are relatively heavyweight anyway so the cost of shared_ptr over unique_ptr is relatively minor.


$ b b

如果您可以考虑某些状态,则可以查看 Flyweight 模式。

这篇关于我应该使用shared_ptr或unique_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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