+ 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; }

27th Aug 2018, 4:56 PM
Suraj Jha
Suraj Jha - avatar
5 Respuestas
+ 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...
27th Aug 2018, 5:27 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 3
thank you Ketan Lalcheta
27th Aug 2018, 5:33 PM
Suraj Jha
Suraj Jha - avatar
+ 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...
27th Aug 2018, 5:19 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
yes it worked
27th Aug 2018, 5:33 PM
Suraj Jha
Suraj Jha - avatar
+ 1
pleasure... happy learning...
27th Aug 2018, 5:40 PM
Ketan Lalcheta
Ketan Lalcheta - avatar