prongs
prongs

Reputation: 9606

C++ need for destructor function

What is The Rule of Three? mentions

After executing the body of the destructor and destroying any automatic objects allocated within the body, a destructor for class X calls the destructors for X's direct [...] members [n3126.pdf 12.4 §6]

Which leaves me wondering, what's the use of a destructor if not to destroy the members? Please provide examples

Upvotes: 1

Views: 275

Answers (5)

Steve Jessop
Steve Jessop

Reputation: 279255

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.

Upvotes: 1

Loki Astari
Loki Astari

Reputation: 264411

It is for additional cleanup that the members themselves are not responsible for. Or in the case of resource management to make sure the resources associated with he object are correctly released. Remember not all members have destructors that will be called (pointers don't have destructors). So if you have pointers you need to manually manage them.

Example resource management with pointers.

shared_ptr::~shared_ptr()
{
    if (decrementReferenceCountAndCheckForZero())
    {
        cleanupResources();
    }
}

Example. Working with frameworks. None of the members know about the framework but the worker does.

MyWorker::MyWorker()
{
     Framwork::Register(this);
}
MyWorker::~MyWorker()
{
    Framework::Unrigester(this);
}

Upvotes: 3

MAG
MAG

Reputation: 3075

This might help you

Suppose a class is having array of someClass which is dynamically created . In your constructor suppose you created

someClass * p = new someClass [10] ;

then in destructor you would write

delete []p ;

Upvotes: 0

Praetorian
Praetorian

Reputation: 109119

All that statement means is if you have a destructor defined as

Foo::~Foo()
{
    Bar b;
    b.do_whatever();
}

then the b object's destructor is run before the destructors of any of Foo's members. The body of the destructor is executed, and the automatic object allocated within the body, i.e. b, is destroyed first.

Upvotes: 1

Alok Save
Alok Save

Reputation: 206536

Anything associated to the class instance that needs disassociation/release/special handling once the object ceases to exist.
Few examples:

  • File handles opened, owned and used by the instance and wont be used post object destruction.
  • Socktes, mutex etc opened and owned by the class instance.

Upvotes: 2

Related Questions