Why this code is printing obj 2 times?.. How's it possible to call destructor by object in main function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why this code is printing obj 2 times?.. How's it possible to call destructor by object in main function?

c++ #include <iostream> using namespace std ; class A{ public : ~A(){ cout <<"obj"; } } ; int main() { A obj; obj.~A(); return 0; } https://code.sololearn.com/cIhjsZ8EZPPP/?ref=app

22nd May 2022, 8:06 PM
proGAMBLER
proGAMBLER - avatar
3 Answers
22nd May 2022, 8:40 PM
Lisa
Lisa - avatar
0
The base class constructor and destructor are called when an object of the derived class is created or deleted. That is, in this case it is enough: int main(){ A obj; }
22nd May 2022, 9:55 PM
Solo
Solo - avatar
0
Oh so the destructor is automatically called when the program ends You called it manually creating the second obj. Idk why there isn't a space tho
23rd May 2022, 1:27 AM
Renz