所以我用这段代码动态分配了一个类对象:
void Inventory::createNewInventoryItem(std::string itemName, unsigned int maxQuantity, unsigned int orderThreshold, double price)
{
InventoryItems* newItem = new InventoryItems;
newItem->createInventoryItem(itemName, maxQuantity, orderThreshold, price);
m_inventory.push_back(newItem);
}
Run Code Online (Sandbox Code Playgroud)
存储在这个向量中: std::vector<InventoryItems*> m_inventory;
然后一旦对象使用完毕就deleteInventoryItem被调用,其定义为:
void Inventory::deleteInventoryItem(int posInVector)
{
m_inventory.at(posInVector)->~InventoryItems();
m_inventory.erase(m_inventory.begin() + posInVector);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:这个deleteInventoryItem函数会成功释放中动态分配使用的内存createNewInventoryItem吗?(这与我在delete[]调用类解构器时是否需要有关的困惑有关)
对于不是“关键任务”的问题,我很抱歉,只是想确保我不会继续不良做法,因为我真的在努力尽可能完美地编程......