I discussed with my coworker about how to delete an entry in a map
the map has the int as the index and a pointer pointing to an object.
I said, first release the object, and then delete the entry.
My coworker said first delete the entry and then release the object.
So what’s the best way? Any trick for this question?
Unless you have a multi-threaded environment, either way would work. The rule of thumb is that, once your function returns, there should be no dangling pointers left, i.e. no pointers to the object that just was deleted.
The only problem that could occur is that if you delete the entry first, you have to make sure to have a temporary copy of the pointer, as you will not be able to retrieve it from the map after the entry has been deleted.