delete things when you don't need them any more. If, for example, you have a vector<int*> and your method pushes new int on it, delete the contents of that vector in the destructor. However if you have a single member int* and a method which assigns a new int to it, deleteing it only in the destructor would leak memory should you call that method more than once. In this case you should delete in the method: |
|
deleteing an invalid pointer will end poorly, but deleteing a null pointer simply does nothing. (If you don't know what's with the :, google "initializer list". It's not necessary here though..)delete the last instance.