I am reading a textbook and it seems to indicate that when you have a member variable that's a pointer, you have to put code in the destructor in order to delete the memory.
It's a little unclear but it seems that the code has to look like this:
private:
double *aPtr;
public:
~NumberArray(){ delete [ ] aPtr;}
Doesn't this end up being 2 delete commands, though, because the destructor is already deleting the first element in that array? Also, do destructors automatically do their default work, even if you have 1 or more lines in your program for the default destructor? Does the program execute your code first or it executes the code the "automatic" part of the destructor?
For some reason I thought that the delete command is just used for dynamically allocated memory. I guess I am wrong about that?
delete[]operator. Also you don't have a default destructor because you overloaded itdeletefor the contents the pointer is pointing to depends. If the class "owns" the pointed contents (and does not share it with other owners) it should. Otherwise, it should not. And, please, consider whether to usedeleteordelete[]depends on the contents the pointer is pointing to.deleteare separate things. Though, adeletecalls the destructor for the pointed object (assuming a non-nullptr), the destructor is also called for any other constructed object. A class which is instanced as local variable is destructed when "going out of scope" -delete-ing it would be illegal. A constructed object in a global variable is destructed after leavingmain()- again nodelete. Not to mention explicit destructor calls for instances which are constructed with placementnew...