I just read recently that its a good practice to implement C++ destructors as virtual functions[1]. Why is this so? Is this a general good practice? If not, in what conditions/cases is a destructor to be made a virtual function?
Reference
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If a base class has a destructor and it is NOT virtual, then any child class’s destructor will not be called if delete is invoked on the base class’s pointer.
This can result in memory leaks.
This will result in:
Any resources that should be deallocated in the triangles destructor has now leaked.