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

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

      <tfoot id='SduHD'></tfoot>
      • <bdo id='SduHD'></bdo><ul id='SduHD'></ul>

        <legend id='SduHD'><style id='SduHD'><dir id='SduHD'><q id='SduHD'></q></dir></style></legend>

        为此获得 boost::shared_ptr

        时间:2023-07-20
          <bdo id='1wK0W'></bdo><ul id='1wK0W'></ul>
          <tfoot id='1wK0W'></tfoot>
          <i id='1wK0W'><tr id='1wK0W'><dt id='1wK0W'><q id='1wK0W'><span id='1wK0W'><b id='1wK0W'><form id='1wK0W'><ins id='1wK0W'></ins><ul id='1wK0W'></ul><sub id='1wK0W'></sub></form><legend id='1wK0W'></legend><bdo id='1wK0W'><pre id='1wK0W'><center id='1wK0W'></center></pre></bdo></b><th id='1wK0W'></th></span></q></dt></tr></i><div id='1wK0W'><tfoot id='1wK0W'></tfoot><dl id='1wK0W'><fieldset id='1wK0W'></fieldset></dl></div>

            <legend id='1wK0W'><style id='1wK0W'><dir id='1wK0W'><q id='1wK0W'></q></dir></style></legend>

              1. <small id='1wK0W'></small><noframes id='1wK0W'>

                    <tbody id='1wK0W'></tbody>
                  本文介绍了为此获得 boost::shared_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在我的代码中广泛使用了 boost:shared_ptr.事实上,在堆上分配的大多数对象都由一个shared_ptr 持有.不幸的是,这意味着我无法将 this 传递给任何采用 shared_ptr 的函数.考虑这个代码:

                  I am making extensive use of boost:shared_ptr in my code. In fact, most of the objects that are allocated on the heap are held by a shared_ptr. Unfortunately this means that I can't pass this into any function that takes a shared_ptr. Consider this code:

                  void bar(boost::shared_ptr<Foo> pFoo)
                  {
                      ...
                  }
                  
                  void Foo::someFunction()
                  {
                      bar(this);
                  }
                  

                  这里有两个问题.首先,这不会编译,因为 shared_ptr 的 T* 构造函数是显式的.其次,如果我强制它使用 bar(boost::shared_ptr<Foo>(this)) 构建,我将创建第二个指向我的对象的共享指针,最终将导致双重删除.

                  There are two problems here. First, this won't compile because the T* constructor for shared_ptr is explicit. Second, if I force it to build with bar(boost::shared_ptr<Foo>(this)) I will have created a second shared pointer to my object that will eventually lead to a double-delete.

                  这让我想到了我的问题:是否有任何标准模式可以从这些对象之一的方法内部获取您知道存在的现有共享指针的副本?在这里使用侵入式引用计数是我唯一的选择吗?

                  This brings me to my question: Is there any standard pattern for getting a copy of the existing shared pointer you know exists from inside a method on one of those objects? Is using intrusive reference counting my only option here?

                  推荐答案

                  您可以从 enable_shared_from_this 然后您可以使用shared_from_this()"而不是this"来生成指向您自己的 self 对象的共享指针.

                  You can derive from enable_shared_from_this and then you can use "shared_from_this()" instead of "this" to spawn a shared pointer to your own self object.

                  链接中的示例:

                  #include <boost/enable_shared_from_this.hpp>
                  
                  class Y: public boost::enable_shared_from_this<Y>
                  {
                  public:
                  
                      shared_ptr<Y> f()
                      {
                          return shared_from_this();
                      }
                  }
                  
                  int main()
                  {
                      shared_ptr<Y> p(new Y);
                      shared_ptr<Y> q = p->f();
                      assert(p == q);
                      assert(!(p < q || q < p)); // p and q must share ownership
                  }
                  

                  当从成员函数生成线程到 boost::bind 到 shared_from_this() 而不是 this 时,这是一个好主意.它将确保对象不被释放.

                  It's a good idea when spawning threads from a member function to boost::bind to a shared_from_this() instead of this. It will ensure that the object is not released.

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

                  上一篇:使用属性树在 Boost 中创建 JSON 数组 下一篇:使用 Boost Asio 异步等待文件描述符

                  相关文章

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

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

                  1. <tfoot id='g2lXO'></tfoot>

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