<tfoot id='8NSxy'></tfoot>

  • <legend id='8NSxy'><style id='8NSxy'><dir id='8NSxy'><q id='8NSxy'></q></dir></style></legend>

      <small id='8NSxy'></small><noframes id='8NSxy'>

      <i id='8NSxy'><tr id='8NSxy'><dt id='8NSxy'><q id='8NSxy'><span id='8NSxy'><b id='8NSxy'><form id='8NSxy'><ins id='8NSxy'></ins><ul id='8NSxy'></ul><sub id='8NSxy'></sub></form><legend id='8NSxy'></legend><bdo id='8NSxy'><pre id='8NSxy'><center id='8NSxy'></center></pre></bdo></b><th id='8NSxy'></th></span></q></dt></tr></i><div id='8NSxy'><tfoot id='8NSxy'></tfoot><dl id='8NSxy'><fieldset id='8NSxy'></fieldset></dl></div>
      • <bdo id='8NSxy'></bdo><ul id='8NSxy'></ul>
      1. 指向对象的指针向量 - 如何避免内存泄漏?

        时间:2023-09-26
      2. <small id='9SmhL'></small><noframes id='9SmhL'>

          <tbody id='9SmhL'></tbody>

            <tfoot id='9SmhL'></tfoot>

                <bdo id='9SmhL'></bdo><ul id='9SmhL'></ul>

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

                  本文介绍了指向对象的指针向量 - 如何避免内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们通常如何处理一个元素是指向对象的指针的向量?我的具体问题是下面提供的代码末尾的注释.谢谢.

                  How do we ususaly deal with a vector whose elements are pointers to object? My specific question is the comment at the end of the code supplied below. Thanks.

                  class A
                  {
                   public:
                   virtual int play() = 0 ; 
                  };
                  
                  class B : public A 
                  {
                  public:
                   int play() {cout << "play in B " << endl;};
                  
                  };
                  
                  class C : public A 
                  {
                  public:
                   int play() {cout << "play in C " << endl;};
                  
                  };
                  
                  
                  int main()
                  {
                  
                      vector<A *> l;
                      l.push_back(new B());
                      l.push_back(new C());
                  
                      for(int i = 0 ; i < l.size();i++)
                      {
                              l[i]->play();
                      }
                  
                      //Do i have to do this to avoid memory leak? It is akward. Any better way to do this? 
                      for(int i = 0 ; i < l.size();i++)
                      {
                              delete l[i];
                      }
                  
                    }
                  

                  推荐答案

                  是的,您必须这样做才能避免内存泄漏.更好的方法是制作一个共享指针向量(boost、C++TR1、C++0x、)

                  Yes, you have to do that to avoid memory leak. The better ways to do that are to make a vector of shared pointers (boost, C++TR1, C++0x, )

                   std::vector<std::tr1::shared_ptr<A> > l;
                  

                  或唯一指针向量 (C++0x) 如果对象实际上不是在此容器和其他对象之间共享

                  or vector of unique pointers (C++0x) if the objects are not actually shared between this container and something else

                   std::vector<std::unique_ptr<A>> l;
                  

                  或使用 boost 指针容器

                    boost::ptr_vector<A> l;
                  

                  PS:不要忘记 A 的虚拟析构函数,正如@Neil Butterworth 所说!

                  PS: Don't forget A's virtual destructor, as per @Neil Butterworth!

                  这篇关于指向对象的指针向量 - 如何避免内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 Valgrind 抑制 Qt 内存泄漏的文件 下一篇:C++ valgrind 在 hello world 中显示内存泄漏

                  相关文章

                • <legend id='afpRK'><style id='afpRK'><dir id='afpRK'><q id='afpRK'></q></dir></style></legend>
                • <small id='afpRK'></small><noframes id='afpRK'>

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

                    1. <tfoot id='afpRK'></tfoot>
                      • <bdo id='afpRK'></bdo><ul id='afpRK'></ul>