我的一个类中有以下构造函数、析构函数和赋值运算符。我想知道它是否会泄漏内存。
MenuItem::MenuItem() {
menu_items = new vector<MenuItem>;
}
MenuItem::MenuItem(const MenuItem &other) {
menu_items = new vector<MenuItem>(*other.menu_items);
}
MenuItem::~MenuItem() {
menu_items->erase(menu_items->begin(), menu_items->end());
delete menu_items;
}
MenuItem & MenuItem::operator= (const MenuItem &other) {
*menu_items = *other.menu_items;
return *this;
}
我主要关心的是赋值运算符。我查看了一些文档并发现:The container preserves its current allocator, which is used to allocate storage in case of reallocation. Any elements held in the container before the call are either assigned to or destroyed.
对我来说,这意味着我的作业不会发生内存泄漏,但我之前误解了文档。谢谢您的帮助。