C ++ 11:用std :: shared_ptr()替换所有非所有的原始指针? [英] C++11: Replace all non-owning raw pointers with std::shared_ptr()?
问题描述
随着 std :: unique_ptr
的到来,瑕疵 std :: auto_ptr
最后可以休息。所以在过去几天,我一直在改变我的代码使用智能指针,并从我的代码中消除所有 delete
。
With the advent of std::unique_ptr
, the blemished std::auto_ptr
can finally be put to rest. So for the last several days, I have been changing my code to use smart pointers and to eliminate all delete
from my code.
虽然valgrind说我的代码是内存干净的,智能指针的语义丰富性将使清洁和更容易理解的代码。
Although valgrind says my code is memory-clean, the semantic richness of smart pointers will make for cleaner and easier-to-understand code.
在大多数代码,翻译很简单:使用 std :: unique_ptr
代替拥有对象持有的原始指针,抛出 delete
,并仔细喷洒
get()
, reset()
和 move ()
,根据需要与其余代码接口。
In most of the code, the translation is simple: use std::unique_ptr
for in place of the raw pointers held by the owning objects, throw out delete
, and carefully sprinkle get()
, reset()
and move()
calls, as needed, to interface well with the rest of the code.
我正在翻译
I am at the point where I am translating non-owning raw pointers to smart pointers now.
由于我小心使用对象的生命周期(我确保我的模块只依赖于一个方向),valgrind告诉我,我没有任何未初始化的读,悬挂指针或泄漏。所以,技术上,我现在可以只留下那些不拥有的原始指针。
Since I was careful with the lifetimes of my objects (I ensure my modules only depend in one direction), valgrind tells me that I don't have any uninitialized reads, dangling pointers, or leaks. So, technically, I could just leave those non-owning raw pointers alone now.
但是,一个选项是更改这些非拥有的原始指针到 std :: shared_ptr
,因为我知道他们是非循环的。
However, one option is to change those non-owning raw pointers to std::shared_ptr
because I know they are acyclic. Or, would it be better to leave them as raw pointers?
我需要一些来自智能指针的老用户的建议,比如你使用什么规则决定是否保留非所有的原始指针,或将它们转换为 std :: shared_ptr
,我经常单元测试和valgrind我的代码。
I need some advice from veteran users of smart pointers as to what rules of thumb you use to decide whether to keep non-owning raw pointers as-is, or to translate them into std::shared_ptr
, keeping in mind that I constantly unit-test and valgrind my code.
编辑:我可能会误会使用 std :: shared_ptr
- 可以与 std :: unique_ptr
结合使用,或者如果我使用 std :: shared_ptr
,所有句柄也应该 std :: shared_ptr
?
I might be misunderstanding the use of std::shared_ptr
- can they be used in conjunction with std::unique_ptr
, or is it the case that if I use std::shared_ptr
, all handles should also be std::shared_ptr
?
推荐答案
就个人而言,这是我(或多或少)做到的:
Personally, this is how I (more or less) do it:
- unique_ptrs 所有权
- 原始指针表示谁给我的原始指针保证该对象的生命周期匹配或超过我的生命周期。
- shared_ptrs 用于共享所有权
- weak_ptrs 用于系统在使用之前要检查对象是否仍然存在。这在我的代码是罕见的,因为我发现它更干净,有一个系统保证任何东西通过它的子系统的生命周期(在这种情况下,我使用一个原始指针)
- unique_ptrs are for sole ownership
- raw pointers mean whoever gave me the raw pointer guarantees the lifetime of that object to match or exceed my lifetime.
- shared_ptrs are for shared ownership
- weak_ptrs are for when a system wants to check if the object still exists before using it. This is rare in my code since I find it cleaner to have a system guarantee the lifetime of anything it passes it's subsystems (in which case I use a raw pointer)
到目前为止,我使用了比shared_ptrs更多的unique_ptr,并且比weak指针更多的原始指针。
By far I use more unique_ptrs than shared_ptrs, and more raw pointers than weak pointers.
这篇关于C ++ 11:用std :: shared_ptr()替换所有非所有的原始指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!