• <tfoot id='3pfj0'></tfoot>
    • <bdo id='3pfj0'></bdo><ul id='3pfj0'></ul>

    <small id='3pfj0'></small><noframes id='3pfj0'>

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

        c ++:可以矢量&lt;Base&gt;包含派生类型的对象

        时间:2023-09-16
        <legend id='uun4H'><style id='uun4H'><dir id='uun4H'><q id='uun4H'></q></dir></style></legend>
          <tbody id='uun4H'></tbody>

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

            <bdo id='uun4H'></bdo><ul id='uun4H'></ul>

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

                • 本文介绍了c ++:可以矢量&lt;Base&gt;包含派生类型的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  标题几乎说明了一切.基本上,这样做是否合法:

                  The title pretty much says it all. Basically, is it legal to do this:

                  class Base {
                      //stuff
                  }
                  
                  class Derived: public Base {
                      //more stuff
                  }
                  
                  vector<Base> foo;
                  Derived bar;
                  foo.push_back(bar);
                  

                  根据我看过的其他帖子,以下内容是可以的,但我不想在这种情况下使用指针,因为它更难使其线程安全.

                  Based on other posts I've seen, the following is okay, but I don't want to use pointers in this case because it's harder to make it thread safe.

                  vector<Base*> foo;
                  Derived* bar = new Derived;
                  foo.push_back(bar);
                  

                  推荐答案

                  不,Derived 对象将是 sliced:所有额外的成员都将被丢弃.

                  No, the Derived objects will be sliced: all additional members will be discarded.

                  使用 std::vector 代替原始指针>.

                  这篇关于c ++:可以矢量&lt;Base&gt;包含派生类型的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:STL 向量和线程安全 下一篇:清理 STL 列表/指针向量

                  相关文章

                  最新文章

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

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

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

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