Someone explain to me why you need a virtual destructor? (In c++) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Someone explain to me why you need a virtual destructor? (In c++)

27th Dec 2017, 10:29 AM
Alexander Sadomov
Alexander Sadomov - avatar
5 Answers
+ 4
here you have the example, comments are in the code guys. https://code.sololearn.com/cgrIXlszu299/#cpp
28th Dec 2017, 11:45 PM
Highman
Highman - avatar
+ 3
because if you create an virtual method which can be overwritten in an inherited class (it can allocate extra memory for example) you need to give a chance to destroy it by appropriate way (it can destroy this extra memory which is unreachable for parent destructor).
27th Dec 2017, 12:27 PM
Highman
Highman - avatar
+ 2
Highman can you give an example?
27th Dec 2017, 2:09 PM
Alexander Sadomov
Alexander Sadomov - avatar
+ 2
Thank you. Evrything became clear.
29th Dec 2017, 10:24 AM
Alexander Sadomov
Alexander Sadomov - avatar
0
Do you know why you need virtual functions in general?
27th Dec 2017, 10:58 AM
Timon Paßlick