Hi, I have a couple of questions.
Let's say I have this code:
class alumno
{
private:
string nombres;
char telefono[12];
int nivel;
public:
alumno();
~alumno();
}
and then in the main I declare
alumno estudiante[10];
My doubts are:
- How do I call the destructor for a single object?
- How do I destroy the entire array?
- If I delete an object, the data remains blank? or does the object box disappear? (that is, if the size of the array is 10, will it go down to 9?)
1. How do I call the destructor for a single object?
Inside an array, you can do
estudiante[3].~alumno( );
2. How do I destroy the entire array?
As you have declared, the array is destroyed on exit of the function, automatically. Before said destruction , the destructors of the objects contained in the array are called, one by one.
If you want to be able to destroy it whenever you please, you have to use dynamic memory:
If you use this last formula, dynamic arrays , remember that it is your responsibility to destroy them by doing the
delete[]
. The compiler completely ignores them. That is, they are not destroyed or released when exiting the function.3. If I delete an object, the data remains blank? or does the object box disappear? (that is, if the size of the array is 10, will it go down to 9?)
If the instances are inside an array , neither the array nor the data changes. Everything is left as it is when the destructor of the class involved ends.
If the destructor does nothing, the memory remains as it was. Of course, the number of elements in the array does not change .
Think that, from the compiler's point of view, an array is nothing more than a block of memory . Once assigned , it does not change size (unless we explicitly change it).
Be careful what you do. By logic, if you first call the destructor of an object, and then do the
delete
whole array destructor, the destructor will be called again . It's up to you to check the logic of your program.Inside the array the objects are stored by value, so there is no need to call an object's destructor. Among other things, the memory occupied by said element within the vector will not be freed.
Calling destroyers directly is something to avoid except for very justified reasons. If you program with certain criteria you will realize that it is not necessary to call them explicitly.
The array is created without using dynamic memory, so you can't manually destroy it. When execution leaves the scope of the array, the program will automatically call the destructor of each object in the array.
A different case would be if a dynamic memory reservation is made. In which case it becomes necessary to free the memory by hand:
Why does this disparate behavior occur?
The array that you have created is stored on the program's stack . The stack is managed by the program itself since it uses it to store the jumps to functions, so the program knows that it must eliminate the variables.
When you use dynamic memory, you become responsible for managing said memory.
No. Since the object does not use dynamic memory, it does not manage unique resources such as files, sockets, etc. you won't notice any difference . The destructor is just one more function that the compiler knows to call under certain circumstances, such as the variable going out of scope or a call to
delete
.