• <tfoot id='Bzjwa'></tfoot>

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

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

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

        获取向量&lt;Derived*&gt;转换为期望向量<

        时间:2023-09-14
        <tfoot id='HVNUu'></tfoot>

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

                  <tbody id='HVNUu'></tbody>

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

                  <legend id='HVNUu'><style id='HVNUu'><dir id='HVNUu'><q id='HVNUu'></q></dir></style></legend>
                  本文介绍了获取向量&lt;Derived*&gt;转换为期望向量<Base*>的函数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  考虑这些类.

                  class Base
                  {
                     ...
                  };
                  
                  class Derived : public Base
                  {
                     ...
                  };
                  

                  这个功能

                  void BaseFoo( std::vector<Base*>vec )
                  {
                      ...
                  }
                  

                  最后是我的向量

                  std::vector<Derived*>derived;
                  

                  我想将 derived 传递给 BaseFoo 函数,但编译器不允许我这样做.如何解决这个问题,而不将整个向量复制到 std::vector?

                  I want to pass derived to function BaseFoo, but the compiler doesn't let me. How do I solve this, without copying the whole vector to a std::vector<Base*>?

                  推荐答案

                  vectorvector 是不相关的类型,所以你不能这样做.这在 C++ FAQ 这里 中有解释.

                  vector<Base*> and vector<Derived*> are unrelated types, so you can't do this. This is explained in the C++ FAQ here.

                  您需要将变量从 vector 更改为 vector 并插入 Derived 对象进入它.

                  You need to change your variable from a vector<Derived*> to a vector<Base*> and insert Derived objects into it.

                  此外,为了避免不必要地复制 vector,您应该通过常量引用而不是值来传递它:

                  Also, to avoid copying the vector unnecessarily, you should pass it by const-reference, not by value:

                  void BaseFoo( const std::vector<Base*>& vec )
                  {
                      ...
                  }
                  

                  最后,为了避免内存泄漏,并使您的代码异常安全,请考虑使用旨在处理堆分配对象的容器,例如:

                  Finally, to avoid memory leaks, and make your code exception-safe, consider using a container designed to handle heap-allocated objects, e.g:

                  #include <boost/ptr_container/ptr_vector.hpp>
                  boost::ptr_vector<Base> vec;
                  

                  或者,更改向量以保存智能指针而不是使用原始指针:

                  Alternatively, change the vector to hold a smart pointer instead of using raw pointers:

                  #include <memory>
                  std::vector< std::shared_ptr<Base*> > vec;
                  

                  #include <boost/shared_ptr.hpp>
                  std::vector< boost::shared_ptr<Base*> > vec;
                  

                  在每种情况下,您都需要相应地修改您的 BaseFoo 函数.

                  In each case, you would need to modify your BaseFoo function accordingly.

                  这篇关于获取向量&lt;Derived*&gt;转换为期望向量<Base*>的函数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:当没有异常时,C++ 异常会以何种方式减慢代码速 下一篇:可以具有 3 种不同数据类型 C++ 的向量

                  相关文章

                  最新文章

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

                2. <small id='vmwf8'></small><noframes id='vmwf8'>

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

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