In case the question is a bit vague, let's use the example class Paper
.
Let's say I have a dynamically allocated array of pointers:
Paper** myFolder;
and later, somewhere, it gets filled up with n pointers:
myFolder = new Paper* [n];
//trimmed - make each pointer in myFolder point to something
When we're done with myFolder
, we need to delete each Paper
before deleting the Paper**
itself:
for(int i = 0; i < n; i++)
delete myfolder[i];
My question: That code isn't very "pretty" now that C++11 and up have for each loops. How can I delete a dynamically allocated array of pointers work with the "for-each" syntax? The following is apparently syntactically incorrect:
for(Paper* p: myFolder)
delete p;
or even
for each (Paper* p in myFolder)
delete p;
Additional Info: myFolder
is not an array of arrays. It is an array of pointers. Why? Because the objective is to take advantage of polymorphism. Paper
may be subclassed, hence myFolder
is an array of pointers rather than an array of objects.
Also: of course std::vector
is a better approach than using raw pointers. The question is theoretical and regards only the new C++ syntax. I'm not looking for advice on how to redo this code.