• <legend id='CIrDZ'><style id='CIrDZ'><dir id='CIrDZ'><q id='CIrDZ'></q></dir></style></legend>

      1. <tfoot id='CIrDZ'></tfoot>
          <bdo id='CIrDZ'></bdo><ul id='CIrDZ'></ul>

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

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

        为现有变量创建一个 boost::shared_ptr

        时间:2023-07-19

            <small id='4ctnC'></small><noframes id='4ctnC'>

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

                  <tbody id='4ctnC'></tbody>
              • <tfoot id='4ctnC'></tfoot>

                  本文介绍了为现有变量创建一个 boost::shared_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个现有的变量,例如

                  I have an existing variable, e.g.

                  int a = 3;
                  

                  我现在如何创建一个 boost::shared_ptra?例如:

                  How can I now create a boost::shared_ptr to a? For example:

                  boost::shared_ptr< int > a_ptr = &a; // this doesn't work
                  

                  推荐答案

                  尽管您应该在变量创建时将其放入托管指针中,以便从现有指针执行此操作.

                  although you should put the variable into a managed pointer on it's creation to do it from an existing pointer.

                  int *a=new int;
                  boost::shared_ptr<int> a_ptr(a);
                  

                  也就是说你绝对不想将堆栈变量放入 shared_ptr 坏事会发生

                  如果由于某种原因一个函数需要 shared_ptr 而你只有一个堆栈变量,你最好这样做:

                  That said you most definitely do not want to be putting stack variables into shared_ptr BAD THINGS WILL HAPPEN

                  If for some reason a function takes shared_ptr and you only have a stack varaible you are better off doing this:

                  int a=9;
                  boost::shared_ptr<int> a_ptr=boost::make_shared(a);
                  

                  看这里:

                  http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/make_shared.html

                  另外值得注意的是,如果您能够使用 shared_ptr,那么它在 c++11 标准中.您可以将 auto 与 make_shared 结合使用,就像构建演讲中的 Herb Sutter 笔记一样.

                  also it is worth noting that shared_ptr is in the c++11 standard if you are able using that. You can use auto in combination with make_shared like Herb Sutter notes in the build talk.

                  #include <memory>
                  
                  int a=9;
                  auto a_ptr=std::make_shared(9);
                  

                  这篇关于为现有变量创建一个 boost::shared_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 C++ Boost 库中,为什么有一个“.ipp"?一些头文件的扩展名 下一篇:如何使用 Boost Filesystem 复制目录

                  相关文章

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

                  • <bdo id='NUES7'></bdo><ul id='NUES7'></ul>
                  1. <legend id='NUES7'><style id='NUES7'><dir id='NUES7'><q id='NUES7'></q></dir></style></legend>
                  2. <tfoot id='NUES7'></tfoot>

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