Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have automatically generated destructors in C++? [closed]

Tags:

c++

c++11

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?

like image 653
user3111311 Avatar asked Jan 09 '14 15:01

user3111311


1 Answers

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.

like image 89
Mike Seymour Avatar answered Sep 16 '22 17:09

Mike Seymour