Reputation: 15798
Say I have a vector of pointers to "Order
" objects. Now I want to remove an order from this vector. I wonder if the following is the right way to remove such a pointer?
std::vector<Order*> orders;
// somehow I obtained a vector of Order*, and an iterator it that points to
// an order that I want to remove completely.
// does the following work?
Order *order = *it;
orders.erase(it);
delete order;
Upvotes: 0
Views: 195
Reputation: 279255
Was the pointer the result of new
? Has anyone else deleted it first? If "yes" and "no", then your code will delete the object.
Upvotes: 3
Reputation: 14119
This will delete the order object. So yes this is the correct way.
Nevertheless you should think about the usage of smart pointers like unique_ptr
or shared_ptr
.
Upvotes: 3
Reputation: 2023
Yes. this is the right way. Containers do not delete their content when it's erased.
Upvotes: 0