<small id='pn7xh'></small><noframes id='pn7xh'>

        <tfoot id='pn7xh'></tfoot>
      1. <i id='pn7xh'><tr id='pn7xh'><dt id='pn7xh'><q id='pn7xh'><span id='pn7xh'><b id='pn7xh'><form id='pn7xh'><ins id='pn7xh'></ins><ul id='pn7xh'></ul><sub id='pn7xh'></sub></form><legend id='pn7xh'></legend><bdo id='pn7xh'><pre id='pn7xh'><center id='pn7xh'></center></pre></bdo></b><th id='pn7xh'></th></span></q></dt></tr></i><div id='pn7xh'><tfoot id='pn7xh'></tfoot><dl id='pn7xh'><fieldset id='pn7xh'></fieldset></dl></div>

        • <bdo id='pn7xh'></bdo><ul id='pn7xh'></ul>
        <legend id='pn7xh'><style id='pn7xh'><dir id='pn7xh'><q id='pn7xh'></q></dir></style></legend>

        C++11:用 std::shared_ptr() 替换所有非拥有的原始指针

        时间:2023-09-15
      2. <small id='Gbqb0'></small><noframes id='Gbqb0'>

          <legend id='Gbqb0'><style id='Gbqb0'><dir id='Gbqb0'><q id='Gbqb0'></q></dir></style></legend>
            <bdo id='Gbqb0'></bdo><ul id='Gbqb0'></ul>
              <tbody id='Gbqb0'></tbody>

            1. <i id='Gbqb0'><tr id='Gbqb0'><dt id='Gbqb0'><q id='Gbqb0'><span id='Gbqb0'><b id='Gbqb0'><form id='Gbqb0'><ins id='Gbqb0'></ins><ul id='Gbqb0'></ul><sub id='Gbqb0'></sub></form><legend id='Gbqb0'></legend><bdo id='Gbqb0'><pre id='Gbqb0'><center id='Gbqb0'></center></pre></bdo></b><th id='Gbqb0'></th></span></q></dt></tr></i><div id='Gbqb0'><tfoot id='Gbqb0'></tfoot><dl id='Gbqb0'><fieldset id='Gbqb0'></fieldset></dl></div>

                  <tfoot id='Gbqb0'></tfoot>
                  本文介绍了C++11:用 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)

                  到目前为止,我使用的 unique_ptrs 比 shared_ptrs 多,原始指针多于弱指针.

                  By far I use more unique_ptrs than shared_ptrs, and more raw pointers than weak pointers.

                  这篇关于C++11:用 std::shared_ptr() 替换所有非拥有的原始指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python 比 C++ 更快更轻吗? 下一篇:C++ 如何在堆栈上动态分配内存?

                  相关文章

                  最新文章

                • <tfoot id='7rPGh'></tfoot>
                    <bdo id='7rPGh'></bdo><ul id='7rPGh'></ul>
                • <legend id='7rPGh'><style id='7rPGh'><dir id='7rPGh'><q id='7rPGh'></q></dir></style></legend>
                    1. <small id='7rPGh'></small><noframes id='7rPGh'>

                      <i id='7rPGh'><tr id='7rPGh'><dt id='7rPGh'><q id='7rPGh'><span id='7rPGh'><b id='7rPGh'><form id='7rPGh'><ins id='7rPGh'></ins><ul id='7rPGh'></ul><sub id='7rPGh'></sub></form><legend id='7rPGh'></legend><bdo id='7rPGh'><pre id='7rPGh'><center id='7rPGh'></center></pre></bdo></b><th id='7rPGh'></th></span></q></dt></tr></i><div id='7rPGh'><tfoot id='7rPGh'></tfoot><dl id='7rPGh'><fieldset id='7rPGh'></fieldset></dl></div>