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

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

      1. <legend id='lPHxN'><style id='lPHxN'><dir id='lPHxN'><q id='lPHxN'></q></dir></style></legend>

        如何启用父代和派生的_shared_from_this

        How to enable_shared_from_this of both parent and derived(如何启用父代和派生的_shared_from_this)
              <tbody id='QGfQp'></tbody>
            <i id='QGfQp'><tr id='QGfQp'><dt id='QGfQp'><q id='QGfQp'><span id='QGfQp'><b id='QGfQp'><form id='QGfQp'><ins id='QGfQp'></ins><ul id='QGfQp'></ul><sub id='QGfQp'></sub></form><legend id='QGfQp'></legend><bdo id='QGfQp'><pre id='QGfQp'><center id='QGfQp'></center></pre></bdo></b><th id='QGfQp'></th></span></q></dt></tr></i><div id='QGfQp'><tfoot id='QGfQp'></tfoot><dl id='QGfQp'><fieldset id='QGfQp'></fieldset></dl></div>

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

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

                • 本文介绍了如何启用父代和派生的_shared_from_this的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有简单的基类和派生类,我希望它们都具有 shared_from_this().

                  I have simple base and derived class that I want both have shared_from_this().

                  这个简单的解决方案:

                  class foo : public enable_shared_from_this<foo> {
                      void foo_do_it()
                      {
                          cout<<"foo::do_it
                  ";
                      }
                  public:
                      virtual function<void()> get_callback()
                      {
                          return boost::bind(&foo::foo_do_it,shared_from_this());
                      }
                      virtual ~foo() {};
                  };
                  
                  class bar1 : public foo , public enable_shared_from_this<bar1> {
                      using enable_shared_from_this<bar1>::shared_from_this;
                      void bar1_do_it()
                      {
                          cout<<"foo::do_it
                  ";
                      }
                  public:
                      virtual function<void()> get_callback()
                      {
                          return boost::bind(&bar1::bar1_do_it,shared_from_this());
                      }
                  };
                  

                  在以下代码中导致异常tr1::bad_weak_ptr:

                  Causes exception tr1::bad_weak_ptr in following code:

                  shared_ptr<foo> ptr(shared_ptr<foo>(new bar1));
                  function<void()> f=ptr->get_callback();
                  f();
                  

                  所以在谷歌搜索"之后,我找到了以下解决方案:

                  So after "googling" I have found following solution:

                  class bar2 : public foo {
                      void bar2_do_it()
                      {
                          cout<<"foo::do_it
                  ";
                      }
                      shared_ptr<bar2> shared_from_this()
                      {
                          return boost::static_pointer_cast<bar2>(foo::shared_from_this());
                      }
                  public:
                      virtual function<void()> get_callback()
                      {
                          return boost::bind(&bar2::bar2_do_it,shared_from_this());
                      }
                  };
                  

                  现在它可以工作了.

                  有没有更好、更方便、更正确的方法来为父母和孩子enable_shared_from_this?

                  Is there any better and more convinient and correct way to enable_shared_from_this for both parent and child?

                  谢谢

                  推荐答案

                  抱歉,没有.

                  问题在于 shared_ptrshared_ptr 是不同的类型.我不了解幕后发生的一切,但我认为当构造函数返回并分配给 shared_ptr 时,内部 weak_ptr 看到没有任何东西指向它(因为只有 shared_ptr 会增加计数器)并重置自己.当您在 get_callback 中调用 bar1::shared_from_this 时,您会收到异常,因为内部 weak_ptr 没有指向任何内容.

                  The problem is that shared_ptr<foo> and shared_ptr<bar1> are different types. I don't understand everything that's going on under the hood, but I think that when the constructor returns and is assigned to a shared_ptr<foo>, the internal weak_ptr<bar1> sees that nothing is pointing to it (because only a shared_ptr<bar1> would increment the counter) and resets itself. When you call bar1::shared_from_this in get_callback, you get the exception because the internal weak_ptr isn't pointing to anything.

                  本质上,enable_shared_from_this 似乎只能从层次结构中的单个类透明地工作.如果您尝试手动实现,问题应该变得很明显.

                  Essentially, enable_shared_from_this only seems to work transparently from a single class in a hierarchy. If you try implementing it manually, the problem should become obvious.

                  这篇关于如何启用父代和派生的_shared_from_this的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                  Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                  How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                  What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                  Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                  How boost auto-linking makes choice?(boost自动链接如何做出选择?)
                    <tbody id='ArqzH'></tbody>

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

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