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;
order == NULL?orderobject has been created withnewand is not shared with other, causing a dangling pointerordervalue (a pointer). Alsodelete orderdoes not changeordervalue. It continue pointing to the same area, even if afterdeleteis is an invalid area.delete order;is permitted to setorderto a null pointer. At least Bjarne Stroustrup says so in his C++ style/FAQ, that might be out of date and I can't be bothered to check ;-) In practice, I don't think any implementation bothers.