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

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

        我可以在 C++ 中拥有具有值语义的多态容器吗?

        Can I have polymorphic containers with value semantics in C++?(我可以在 C++ 中拥有具有值语义的多态容器吗?)
            <tbody id='4liN9'></tbody>

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

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

                  本文介绍了我可以在 C++ 中拥有具有值语义的多态容器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  作为一般规则,我更喜欢在 C++ 中使用值而不是指针语义(即使用 vector 而不是 vector).通常性能上的轻微损失可以通过不必记住删除动态分配的对象来弥补.

                  As a general rule, I prefer using value rather than pointer semantics in C++ (ie using vector<Class> instead of vector<Class*>). Usually the slight loss in performance is more than made up for by not having to remember to delete dynamically allocated objects.

                  不幸的是,当您想要存储从公共基础派生的各种对象类型时,值集合不起作用.请参阅下面的示例.

                  Unfortunately, value collections don't work when you want to store a variety of object types that all derive from a common base. See the example below.

                  #include <iostream>
                  
                  using namespace std;
                  
                  class Parent
                  {
                      public:
                          Parent() : parent_mem(1) {}
                          virtual void write() { cout << "Parent: " << parent_mem << endl; }
                          int parent_mem;
                  };
                  
                  class Child : public Parent
                  {
                      public:
                          Child() : child_mem(2) { parent_mem = 2; }
                          void write() { cout << "Child: " << parent_mem << ", " << child_mem << endl; }
                  
                          int child_mem;
                  };
                  
                  int main(int, char**)
                  {
                      // I can have a polymorphic container with pointer semantics
                      vector<Parent*> pointerVec;
                  
                      pointerVec.push_back(new Parent());
                      pointerVec.push_back(new Child());
                  
                      pointerVec[0]->write(); 
                      pointerVec[1]->write(); 
                  
                      // Output:
                      //
                      // Parent: 1
                      // Child: 2, 2
                  
                      // But I can't do it with value semantics
                  
                      vector<Parent> valueVec;
                  
                      valueVec.push_back(Parent());
                      valueVec.push_back(Child());    // gets turned into a Parent object :(
                  
                      valueVec[0].write();    
                      valueVec[1].write();    
                  
                      // Output:
                      // 
                      // Parent: 1
                      // Parent: 2
                  
                  }
                  

                  我的问题是:我可以拥有我的蛋糕(价值语义)并吃掉它(多态容器)吗?还是我必须使用指针?

                  My question is: Can I have have my cake (value semantics) and eat it too (polymorphic containers)? Or do I have to use pointers?

                  推荐答案

                  由于不同类的对象会有不同的大小,如果将它们存储为值,最终会遇到切片问题.

                  Since the objects of different classes will have different sizes, you would end up running into the slicing problem if you store them as values.

                  一种合理的解决方案是存储容器安全的智能指针.我通常使用 boost::shared_ptr ,它可以安全地存储在容器中.请注意 std::auto_ptr 不是.

                  One reasonable solution is to store container safe smart pointers. I normally use boost::shared_ptr which is safe to store in a container. Note that std::auto_ptr is not.

                  vector<shared_ptr<Parent>> vec;
                  vec.push_back(shared_ptr<Parent>(new Child()));
                  

                  shared_ptr 使用引用计数,因此在删除所有引用之前它不会删除底层实例.

                  shared_ptr uses reference counting so it will not delete the underlying instance until all references are removed.

                  这篇关于我可以在 C++ 中拥有具有值语义的多态容器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                  • <tfoot id='Eajva'></tfoot>
                      <tbody id='Eajva'></tbody>

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

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

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