我将对象类tipo推到一个向量中,当我推第一个时,构造函数被调用(因为它应该)并且析构函数被立即调用(我认为不应该发生)。然后当我推送下一个对象时,构造函数被调用一次,析构函数被调用两次,然后第三次调用三次,依此类推。
每次我将某些东西推送到向量时,似乎都会调用更多次析构函数。
这是我的课:
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
难道我做错了什么?为什么会发生这种情况?