Is it possible to have automatically generated destructors in C++?
It is too big burden to do it by ourselves all the time. Is it so hard for the compiler to generate destructors? Can't it detect what is a "resource" and release it in the destructor?
Certainly it is, and that's exactly what the language does. If you don't declare a destructor, then one will be generated for you: it will invoke the destructor of each member and base subobject.
You only need to write your own destructor if you're managing a resource that isn't released automatically; for example, a raw pointer to something that you allocated with new
. You shouldn't need such a thing in most classes - use containers, smart pointers and other RAII types to manage these automatically for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With