Why destructors are introduced?without destructors memory can be erased? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula gr√°tis
0

Why destructors are introduced?without destructors memory can be erased?

Is it possible?

24th Jul 2019, 6:18 AM
vishal more
vishal more - avatar
2 Respostas
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.
24th Jul 2019, 6:30 AM
Seb TheS
Seb TheS - avatar
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.
24th Jul 2019, 7:36 AM
James
James - avatar