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

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

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

        时间:2023-10-07

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

              <tfoot id='pFdBw'></tfoot>

                    <tbody id='pFdBw'></tbody>

                  本文介绍了将元素添加到抽象类的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 向量,返回与参数 下一篇:函数调用中的单元素向量初始化

                  相关文章

                  最新文章

                • <small id='1A3wV'></small><noframes id='1A3wV'>

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

                      <bdo id='1A3wV'></bdo><ul id='1A3wV'></ul>

                    1. <tfoot id='1A3wV'></tfoot>