5 Replies - 1451 Views - Last Post: 09 December 2012 - 09:19 AM Rate Topic: -----

#1 jakes9x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-December 12

When does a destructor run in C++

Posted 09 December 2012 - 04:46 AM

Is it important to define destructor in your program Or is it called automatically when costructors are used and is it only called when constructors are used?

#include<iostream>
class sample
{
int a, b;
public:
sample()
{
a=1;
b=2;
cout<<"a = "<<a << " b = "<<b <<endl;
}

sample(int x)
{
a = x;
b = x+1;


}

sample(int x, int y)
{
a = x+y;
b = y+10;
}

~sample()
{
cout<<"Destructor "<<endl;
}

void disp()
{
cout<<"a = "<<a << " b = "<<b <<endl;
}

};

main()
{
sample P;
sample A(3);
A.disp();
sample B(5,5);
B.disp();
}



The output of program will be

a = 1 b = 2
a = 3 b = 4
a = 10 b = 15
Destructor
Destructor
Destructor

Why the output is not like this First constructor does its job then destructor runs, then second constructor does its job then again destructor runs and so on.
like this
a = 1 b = 2
Destructor
a = 3 b = 4
Destructor
a = 10 b = 15
Destructor

How does it actually work what is the order

MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 09 December 2012 - 04:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: When does a destructor run in C++

#2 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 625
  • Joined: 11-June 11

Re: When does a destructor run in C++

Posted 09 December 2012 - 05:07 AM

Destructor is executed when the class object is destroyed. When an object goes out of scope, or a dynamically allocated object is explicitly deleted using the delete keyword, the class destructor is called to help clean up the class before it is removed from memory. For simple classes the destructor is not needed, but if you want to do something important, like closing open files or some other task, you can do it in the destructor...

As for why the program executes the destructors in the end is because the objects you have created goes out of scope once the control reaches to end of main i.e. the right closing brace... So, the objects go out of scope and their destructors are called... If you want to see the destructor earlier, try to allocate an object with new and then delete it, you will see the destructor gets called earlier...
Was This Post Helpful? 1
  • +
  • -

#3 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: When does a destructor run in C++

Posted 09 December 2012 - 08:42 AM

See scope of variables.

edit: And definitely see C++ FAQ Lite entries on destructors.

Play with the code below:
Spoiler

This post has been edited by Xupicor: 09 December 2012 - 09:38 AM

Was This Post Helpful? 3
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 625
  • Joined: 11-June 11

Re: When does a destructor run in C++

Posted 09 December 2012 - 08:57 AM

@Xupicor, Excellent example! I'm gonna save this code... ;)
Was This Post Helpful? 0
  • +
  • -

#5 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: When does a destructor run in C++

Posted 09 December 2012 - 09:11 AM

One thing to note is that you can arbitrarily create scopes. Sometimes people like to limit the life of a variable so they do something like:
int main()
{
    // ... do some work
    {
        // create some variables and do some work
    }
}


Any variables declared inside the nested curly braces will be constructed when the code reaches the opening curly-brace, and the destructors will be run at the end of the nested curly braces.

This post has been edited by vividexstance: 09 December 2012 - 09:12 AM

Was This Post Helpful? 1
  • +
  • -

#6 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: When does a destructor run in C++

Posted 09 December 2012 - 09:19 AM

@OP, and my example

That goes without saying that if you want to know what happens you really should get some good source (recommended C++ books, ISO/IEC C++ standard if your stomach is up to it) and read about what is guaranteed and what is not.

TIAS (Try It And See) is great, but remember that with C and C++ just because code compiles and "works" doesn't mean it's valid. Also, just because your compiler does some things some way doesn't always mean that some other won't do it differently. There are things that are guaranteed by the language standard, but... Undefined behaviours are not as hard to invoke as you'd think, so beware. ;)

TL;DR: If you find yourself guessing at something, then stop and check source material.

This post has been edited by Xupicor: 09 December 2012 - 09:21 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1