If the "Destructors" are used only for deleting an object then why do they have a body? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

If the "Destructors" are used only for deleting an object then why do they have a body?

12th Jun 2020, 3:34 AM
Prabhanshu Chaturvedi
6 Answers
+ 2
A destructor prepares a single instance (object) for its (complete) deletion. This will not delete all existing objects of this class, but all members of this specific instance.
12th Jun 2020, 3:53 AM
Sandra Meyer
Sandra Meyer - avatar
+ 6
Imagine you own 3 drones, but you need only 2. So you want to sell one, find a buyer and get rid of it. But before deactivating it, you might want to get sure, that it's not still in the air or has pictures of your wife stored on its SD card, right? And exactly that's a destructors job - to prepare an object for it's deletion. Loose the connections to other objects, delete dependent data etc.
12th Jun 2020, 3:47 AM
Sandra Meyer
Sandra Meyer - avatar
+ 5
I get it now, thank you.
12th Jun 2020, 3:55 AM
Prabhanshu Chaturvedi
+ 4
Oh, I see. So it deletes particular objects and not all of them.
12th Jun 2020, 3:50 AM
Prabhanshu Chaturvedi
+ 4
The memory assigned to that instance of class
12th Jun 2020, 3:54 AM
Prabhanshu Chaturvedi
+ 1
You're welcome
12th Jun 2020, 3:57 AM
Sandra Meyer
Sandra Meyer - avatar