How destructor works while object is deleted?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How destructor works while object is deleted??

17th Aug 2018, 7:11 AM
Rikshith U Uchil
Rikshith U Uchil - avatar
3 Answers
+ 3
when you delete an object that was created in the heap, or when an object define as a local variable gets out of scope, the destructor is called. if your object is complex, specifically, if its class has members allocated in the heap with new, the destructor code should clean (delete) those members up as to not leave memory leaks.
17th Aug 2018, 7:18 AM
ifl
ifl - avatar
+ 1
c++
17th Aug 2018, 7:13 AM
Rikshith U Uchil
Rikshith U Uchil - avatar
+ 1
Rikki when the scope of object is over , destructor is automatically gets called by compiler... in same scope for more than one objects, it's order to get destroyed is reverse than object creation order.. for example, if you have created obj1 first and obj2 last, at the end of main function; first obj2 will be destroyed by destructor and then obj1...
17th Aug 2018, 7:17 AM
Ketan Lalcheta
Ketan Lalcheta - avatar