So I have a pointer to an array of pointers. If I delete it like this:
delete [] PointerToPointers;
Will that delete all the pointed to pointers as well? If not, do I have to loop over all of the pointers and delete them as well, or is there an easier way to do it? My google-fu doesn’t seem to give me any good answers to this question.
(And yeah, I know I need to use a vector. This is one of those ‘catch up on C++’ type assignments in school.)
Yes you have to loop over the pointers, deleting individually.
Reason: What if other code had pointers to the objects in your array? The C++ compiler doesn’t know if that’s true or not, so you have to be explicit.
For an ‘easier way,’ two suggestions: (1) Make a subroutine for this purpose so at least you won’t have to write the code more than once. (2) Use the ‘smart pointer’ design paradigm where you hold an array of objects with reference-counters, then the objects are deleted when the objects are no longer referenced by any code.