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

        • <bdo id='g5vqI'></bdo><ul id='g5vqI'></ul>
      1. <legend id='g5vqI'><style id='g5vqI'><dir id='g5vqI'><q id='g5vqI'></q></dir></style></legend>

      2. 将元素添加到抽象类的 std::vector

        Adding Elements to std::vector of an abstract class(将元素添加到抽象类的 std::vector)

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

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

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

                1. 本文介绍了将元素添加到抽象类的 std::vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想将从公共接口(抽象类)派生的类的对象存储在该抽象类的 std::vector 中.这个向量应该填充在一个循环中,通常我会调用一个类的构造函数并将创建的对象推入向量中.

                  I want to store objects of classes derived from a common interface (abstract class) in a std::vector of that abstract class. This vector should be filled in a loop and usually I would call the constructor of a class and push the created object into the vector.

                  据我所知,在抽象类的情况下,我只能存储指向该类的指针,因此我需要 push_back 派生类的指针.但是,我不确定这些新创建的对象的范围.

                  As I understand, in case of an abstract class I can only store pointers to that class, so I need to push_back pointers of the derived classes. However, I am not sure about the scope of these newly created objects.

                  请看下面的代码.此代码编译并运行良好,但我的问题是:

                  Please, have a look at the code below. This code compiles and works fine but my questions are:

                  a) 对象是否保证存在于主函数的第二个 for 循环中?或者它们可能会在创建它们的循环范围之外停止存在?

                  a) Are the objects guaranteed to exist in the second for-loop in the main function? Or might they cease existing beyond the scope of the loop in which they are created?

                  b) 是否调用了所有对象的析构函数或可能存在内存泄漏?

                  b) Are all objects' destructors called or might there be memory leaks?

                  #include<vector>
                  #include<iostream>
                  class Interface {
                      public:
                      Interface( int y ) : x(y) {}
                      virtual ~Interface() {}
                      virtual void f() = 0;
                      int x;  
                  };
                  
                  class Derived_A : public Interface {
                      public:
                      Derived_A( int y ) : Interface(y) {}
                      void f(){ return; }
                  };
                  
                  class Derived_B : public Interface {
                      public:
                      Derived_B( int y ) : Interface(y) {}
                      void f(){ return; }
                  };
                  
                  
                  int main()
                  {
                      std::vector<Interface*> abstractObjects;
                      int N = 5;
                      for(int ii = 0; ii < N; ii++ )
                      {
                          abstractObjects.push_back( new Derived_A(ii) );
                          abstractObjects.push_back( new Derived_B(ii) );
                      }
                  
                      for(int ii = 0; ii < abstractObjects.size(); ii++ )
                      {
                          abstractObjects[ii]->f();
                          std::cout << abstractObjects[ii]->x << '	' << std::endl;
                      }
                  
                  
                      for(int ii = 0; ii < abstractObjects.size(); ii++ )
                      {
                          delete abstractObjects[ii];
                      }
                  
                      return 0;
                  }
                  

                  推荐答案

                  这是智能指针的完美案例.您可以将指针存储在 unique_ptr 中,这是一种 RAII 类型.当 unique_ptr 超出范围时,它会自动为您删除内存.

                  This is a perfect case for smart pointers. You can store the pointers in a unique_ptr which is a RAII type. When the unique_ptr goes out of scope it will autmaticlly delete the memory for you.

                      //...
                      std::vector<std::unique_ptr<Interface>> abstractObjects;
                      int N = 5;
                      for(int ii = 0; ii < N; ii++ )
                      {
                          abstractObjects.push_back( std::make_unique<Derived_A>(ii) );
                          abstractObjects.push_back( std::make_unique<Derived_B>(ii) );
                      }
                  
                      for(auto & e : abstractObjects)  // ranged based for loop
                      {
                          e->f();
                          std::cout << e->x << '	' << std::endl;
                      }
                      // no need to do anything here.  the vector will get rid of each unique_ptr and each unique_ptr will delete each pointer
                      return 0;
                  }
                  

                  这篇关于将元素添加到抽象类的 std::vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                      <tbody id='VmrSa'></tbody>

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

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

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