0
Why destructors are introduced?without destructors memory can be erased?
Is it possible?
2 odpowiedzi
0
As Pythoner I'd say that destructor is just a function that is called when the object is destroyed, it does not destroy it.
But there can also be some hidden predefined destructors, that focuses on removing the object data.
0
A default destructor will be used if you don't create one yes but say you have a pointer to dynamically allocated memory inside your class. You need to write your own destructor to make sure the memory is freed up before you destroy a class instance other wise it goes out of scope and is destroyed but causes a memory leak. By default the pointer can be destroyed but what about what it was pointing to?
{
Object* obj = new Object;
}
with not destructor when it goes out of scope the obj pointer is destroyed but not the object it points too.