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

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

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

      1. std::vector 在 push_back 期间多次调用析构函数?

        std::vector calling destructor multiple times during push_back?(std::vector 在 push_back 期间多次调用析构函数?)
        <i id='U7ujt'><tr id='U7ujt'><dt id='U7ujt'><q id='U7ujt'><span id='U7ujt'><b id='U7ujt'><form id='U7ujt'><ins id='U7ujt'></ins><ul id='U7ujt'></ul><sub id='U7ujt'></sub></form><legend id='U7ujt'></legend><bdo id='U7ujt'><pre id='U7ujt'><center id='U7ujt'></center></pre></bdo></b><th id='U7ujt'></th></span></q></dt></tr></i><div id='U7ujt'><tfoot id='U7ujt'></tfoot><dl id='U7ujt'><fieldset id='U7ujt'></fieldset></dl></div>

              <tbody id='U7ujt'></tbody>
            <legend id='U7ujt'><style id='U7ujt'><dir id='U7ujt'><q id='U7ujt'></q></dir></style></legend>

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

                • <tfoot id='U7ujt'></tfoot>

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

                  本文介绍了std::vector 在 push_back 期间多次调用析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我将对象类tipo 推送到向量,当我推送第一个时,构造函数被调用(它应该被调用)并且析构函数被立即调用(我认为这不应该发生).然后当我推送下一个对象时,构造函数被调用一次,析构函数被调用两次,然后在第三次调用三次,依此类推.

                  I'm pushing objects class tipo to a vector, when I push the first one the constructor get's called (as it should) and the destructor is called immediatelly (which I don't think should happen). Then when I push the next object the constructor is called once and the destructor is called twice, then on the third three times and so on.

                  似乎每次我向向量推送内容时都会多次调用析构函数.

                  It seems that the destructor is called more times every time I push something to the vector.

                  这是我的课:

                  class Item
                  {
                  protected:
                  ...
                  public:
                      Item();
                      Item(char * no, int hit, int ve, char * pathTilesheet, int an, int al, bool inv, bool vol, bool fan, int mh = NULL);
                      ~Item();
                  };
                  
                  
                  Item::Item(char *no, int hi, int ve, char *pathTilesheet, int an, int al, bool inv, bool vol, bool fan, int mh){    
                      // CARGAR SDL
                      tileSheet = load_image(pathTilesheet);
                      tileSheetEspejo = flip_surface(tileSheet, FLIP_HORIZONTAL);
                  }
                  

                  这是正在发生的事情:

                  std::vector<Item> vecItems;
                  vecItems.push_back(Item("life",4,0,"assets/imagenes/hp.png", 8, 8, false, false, false));
                  // HERE THE CONSTRUCTOR AND THE DESTRUCTOR ARE CALLED
                  vecItems.push_back(Item("lifeXL",8,0,"assets/imagenes/hp-xl.png", 16, 16, false, false, false));
                  // HERE THE CONSTRUCTOR IS CALLED ONCE AND THE DESTRUCTOR TWICE
                  vecItems.push_back(Item("blast 1",-4,14,"assets/imagenes/bola.png", 8, 8, false, true, false));
                  // HERE THE CONSTRUCTOR IS CALLED ONCE AND THE DESTRUCTOR MULTIPLE TIMES
                  

                  我做错了吗?为什么会发生这种情况?

                  Am I doing something wrong? Why could this be happening?

                  推荐答案

                  你的每一行代码都会创建一个临时的Item,将它复制到向量的内存中,然后销毁临时的.这就是为什么你每次看到(至少)一个析构函数调用.

                  Each line of your code creates a temporary Item, copies it into the vector's memory, and then destroys the temporary. That's why you see (at least) one destructor call each time.

                  在 C++11 中,您可以通过使用 emplace_back(args...) 而不是 push_back(Item(args...)) 来避免创建和销毁临时文件code>,直接在vector的内存中创建对象.

                  In C++11, you could avoid creating and destroying the temporary by using emplace_back(args...) rather than push_back(Item(args...)), to create the object directly in the vector's memory.

                  此外,向量有时需要增长,重新分配更大的内存块,以便将其所有元素保留在连续数组中.当它这样做时,每个元素都被移动到新内存中,旧元素被销毁.这就是为什么您有时会看到不止一个析构函数调用.

                  Additionally, the vector sometimes needs to grow, reallocating a larger block of memory, in order to keep all its elements in a contiguous array. When it does that, each element is moved into the new memory, and the old elements are destroyed. That's why you sometimes see more than one destructor call.

                  如果您知道向量的最终大小,则可以避免重新分配的需要,方法是在开始之前调用 reserve() 分配足够的内存.或者,还有像 dequelist 这样的容器,它们不会随着元素的增长而移动它们的元素,但对于其他操作来说可能效率较低.

                  You can avoid the need for reallocation, if you know the final size of the vector, by calling reserve() to allocate enough memory before you start. Alternatively, there are containers like deque and list which don't move their elements as they grow, but which may be less efficient for other operations.

                  顺便说一句,如评论中所述,如果该类正在管理资源(这意味着存在析构函数),您可能需要提供或删除复制构造函数和复制赋值运算符三法则,也许考虑让它移动以提高效率.

                  As an aside, as noted in the comments, if the class is managing resources (which is implied by the presence of a destructor), you probably need to provide or delete the copy constructor and copy-assignment operator per the Rule of Three, and perhaps think about making it movable for efficiency.

                  这篇关于std::vector 在 push_back 期间多次调用析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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++ 中生成随机数的最佳方法是什么?)

                    • <small id='qDpi5'></small><noframes id='qDpi5'>

                        <tbody id='qDpi5'></tbody>

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