Hello, following is the test program I created.
I have tried to dynamically create objects of derived classes of the base class "person" by using a pointer array of 100 size..
But, the program is giving RunTime error whenever it comes to deletion of the pointer array. Now, I don't understand why is this happening.. Can anyone explain, please? Thank you!
and maybe more fundamentally:
p is an array of pointers.
p is not, itself, a pointer at all!
conceptually, you have this:
unsigned long long p[100];
where p[x] is a number that just happens to be the address in ram of some other variable.
you wouldn't think to use delete[] on p if it were just an array of long long, right? But that is exactly what it really IS. The syntax is tripping you up because it has an * in it maybe. It takes some getting used to.
one last time, just to be extra clear: (person *)p[100]; that is the TYPE of the array, its an array of pointers to person.
so the array contains pointers, but isnt, itself, a pointer.
person* p; //this one IS a pointer itself. its the array-ness of the above that changes how to read it, a bit. you would still need a new on this if you wanted to delete it.
back to the original,
p[x] IS a pointer.
if you had happened to code
p[x] = new thingy[somesize];
then
delete[] p[x]; would be a valid thing to do.
if you had declared p as thingy **p;
p = new thingy*[100];
now you can delete p, because p is now actually a pointer that is doing the same thing as your array (there is no reason to do this if 100 is a fixed size you want to stick to).
I suggest you put this program down and play with pointers in a 10 line main program for a few min to get a better handle on the syntax and truth behind what you are trying to do. Trying to do something bigger while using pointers while not understanding them fully is a recipe for disaster. And doubly so when trying to do this in classes with inheritance and other complexities.