std :: vector在push_back期间多次调用析构函数? [英] std::vector calling destructor multiple times during push_back?

查看:254
本文介绍了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.

似乎析构函数是

这是我的课程:

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 ...)),直接在向量的内存中创建对象

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.

如果知道向量的最终大小,则可以通过调用<$ c $来避免重新分配的需要。 c> reserve()在开始之前分配足够的内存。另外,还有诸如 deque list 之类的容器,它们不会随着元素的增长而移动,但可能

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期间多次调用析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆