Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
#include<iostream.h>
#include<conio.h>

class x
{
      public:
             x()
             {
                 cout<<"constructor called";
             }
             void show()
             {
                  cout<<"Show() called";
             }
             ~x()
             {
                 cout<<"Destructor called";
             }
};

int main()
{
    x o;
    o.show();
    getch();
    return 0;
}
Posted
Updated 3-Aug-11 9:38am
v3
Comments
Maximilien 3-Aug-11 11:44am    
On VS2008 the destructor gets called; I would assume it does get called in every modern C++ compilter.
Put a breakpoint in the destructor; it should be triggered.

Have checked by executing the program>Which compiler you are using?I am sure destructor will be called.
 
Share this answer
 
Comments
Ashutosh_g 3-Aug-11 11:01am    
i am using dev cpp compiler.
Sergey Alexandrovich Kryukov 3-Aug-11 14:42pm    
Agree (my 5), but I don't think it has anything to do with a specific compiler -- please see the answer by Nemanja Trifunovic and my comment to it.
--SA
I bet it is called - you just don't see it because it gets executed after the getch() function.

Why don't you put a getch() at the end of your destructor? You'll see the message then.
 
Share this answer
 
Comments
Ashutosh_g 3-Aug-11 10:58am    
i put the getch method at the end of destructor function but result was not displayed.........
mbue 3-Aug-11 13:00pm    
No you didnt. You have to do this in that way:

int main()
{
{
x o;
o.show();
} // <-- destructor called
getch();
return 0;
}
Sergey Alexandrovich Kryukov 3-Aug-11 14:39pm    
I think you're right but the idea of moving "getch()" to the destructor is weird -- OP could think this is a valid programming technique, not just the experiment as you probably mean. It's much better to recommend using debugger :-).
Another way would be re-directing console output:
>program.exe > output_file.txt
I voted 4.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900