+ 2
why destructor in this code doesn't runs in visual studio's console application
#include "stdafx.h" #include <iostream> using namespace std; class ratio { public: ratio(); void display(); ~ratio(); }; ratio::ratio() { cout << "\n OBJECT IS BORN"; } void ratio::display() { cout << "\n OBJECT IS ALIVE"; } ratio :: ~ratio() { cout << "\n OBJECT DIES \n\n"; } int main() { ratio x; x.display(); system("pause"); return 0; }
5 ответов
+ 3
Suraj if you wanna check that behaviour in output, update your code in main as below :
{ratio x;
x.display();
}
system("pause");
here, {} allows object get destroyed before system command is executed... why because scope of object is bounded within {} and as system command is reached, object scope is over to check destructor behavior...
+ 3
thank you Ketan Lalcheta
+ 2
Suraj system command is the one.... in first line, you have created a object which is going to get destroyed at end of entire code execution...
your code ends at return 0 which initiate destruction call... but system command don't allow to reach code to that line and hence you are not able to check that result on VS...
+ 2
yes it worked
+ 1
pleasure... happy learning...