0

Just started learning C++ and I'm confused on how to program a deconstructor. So far I understand that a deconstructor is called whenever an object is destroyed, which from my understanding is when the program ends and C++ automatically destroys all objects. Does this just mean that I should just re-intialize all variables back to what they originally were?

So is this what I should have:

/*In the Header file*/
List();
~List();

//Other function prototypes go here


/*In the Implementation file*/
List::List()
{
  head = NULL;
  tail = NULL;
  count = 1;
}

List::~List
{
  head = NULL;
  tail = NULL;
  count = 1;
}

//Other function bodies go here
giraffesyo
  • 4,860
  • 1
  • 29
  • 39
sgmm
  • 580
  • 2
  • 7
  • 13
  • I have a hard time believing this is not a duplicate, but seeing as I can't find one, go nuts, folks. Should answer most of your questions: https://isocpp.org/wiki/faq/dtors. – user4581301 Sep 06 '15 at 00:37
  • possible duplicate of [Practical application of class destructor](http://stackoverflow.com/questions/10310513/practical-application-of-class-destructor) – Fabio Sep 06 '15 at 02:03
  • @user4581301 there are other questions w the answer that are just not named the same. – Fabio Sep 06 '15 at 02:04

3 Answers3

2

No. The destructor is for deallocating/cleaning up any resources that the object might be using. For instance file handles need to be closed, UI resources need to be released back to the system, memory that you have newed needs to be deleteed.

In your example count seems like an int, so you don't need to do anything for that as the memory for that is a part of the object being destroyed, but if you have allocated memory for your list you will have to deallocated it in the destructor. Depending on how you have constructed the list you might want to iterate through the list and deallocate each node.

Dominique McDonnell
  • 2,510
  • 16
  • 25
1

To clear up one misconception, the destructor is called any time an object goes out of scope (a local variable hits the end of the code block in which it was created). If an object was created in global scope (outside of a function) it will be destroyed at program exit. Think of this as a big set of braces around the whole program.

void function(object a)
{
    object b;
    int x = 0;
    while (x < 10)
    {
        object c;
        // La la la Doing stuff.
    } // c destroyed here. It will be created and destroyed for each run through the loop
    a = b;
} //b destroyed here 
// a also destroyed here, so watch out passing things into functions if you want them 
// back changed.

or when it is manually destroyed with the delete command

object * d = new object();
// la la la doing stuff
delete d;

Since d was manually created, it must be manually destroyed. Watch out C# and Java people.

What do you need to do in a destructor?

Put everything away. Don't waste your time setting values back to their defaults. They won't be around long enough to appreciate the gesture. But if you opened a file, and still have it open, close it. If you newed other objects, delete them. If you have a mutex locked, unlock it. If you are managing threads, you probably want to notify the threads and wait for them to finish before finishing. But do it with a time-out so you don't hang on a stuck thread.

While we're on the topic, read this: What is The Rule of Three?

Put simply The Rule of Three means if you have a destructor, a copy constructor or an assignment operator (operator=), you almost certainly require all three. A lot of time you'll see people getting odd memory errors because they didn't obey the Rule of Three. Don't be that guy.

When you have The Rule of Three down, go looking for the Rule of Five.

How this applies to the OP:

List::~List
{
  head = NULL; //very probably need to delete this pointer
  tail = NULL; //and possibly this one
  count = 1; // don't need this at all
}

So it should probably look like:

List::~List
{
    if (head != NULL)
    {
        delete head; // This should start a chain reaction deleting all of the linked 
                     // items in the list, ensuring that they have all been freed
    }
}

In addition, the OP should have a copy constructor and an assignment operator that make sure that when the List is deleted it doesn't also obliterate the data of any copies of the list.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
0

It is called destructor not deconstructor

A destructor code is generally defines how you may end the life of an instance of a class.

Usually this will include memory deallocation or freeing as well as resource freeing 'closing a file handle', unlocking a synchroniser, it might also include more complicated stuff like interrupting a thread to end it's life if this thread life cycle is inside the class.. it might have more than that like notifying about the destruction of a class to whomever have assigned for a notification .. there is a lot of code that might be required when you end an instance of a class without a destructor the encapsulation of OOP would have suffered

Last note in c++ the new, and delete reserved words are memory allocators that use the constructor/destructor of an instance of an object

I hope it is clear enough

Ibrahim Magdy
  • 343
  • 1
  • 3
  • 14