Your class might manage resources that aren't released by calling the destructors of the data members of your object. If so, then the code to release the resource belongs in a destructor that you write.
For example if you allocate objects with new
then they must be freed with delete
. If you open a file with fopen
then it's closed with fclose
. If you take a Posix mutex with pthread_mutex_lock
then it must be released with pthread_mutex_unlock
.
For each kind of resource needs freeing you (or someone else) can write a class that manages and frees that resource, and provides access to its basic operations. Hence the existence of classes like std::unique_ptr
, std::shared_ptr
, std::lock_guard
, std::fstream
. Of course, for simplicity you usually want there to be only one class that manages a particular kind of resource. So, since std::lock_guard
exists in C++11, the only reason that you'd write your own class to release a mutex would be if you're providing some alternative interface to the standard one. Classes with non-default destructors should ideally be rare in your own code -- often there already exist classes that you can use as data members or automatic variables, and whose destructors do the job.