Why does this code print A twice? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why does this code print A twice?

#include <iostream> using namespace std; class A { public: ~A(){ cout<<"A"; } }; int main() { A obj; obj.~A(); }

8th Jun 2018, 10:44 AM
Rabilu Ibrahim Muhammad
Rabilu Ibrahim Muhammad - avatar
1 Answer
+ 11
Rabilu Muhammad Ibrahim A obj ; //object created obj.~A(); //calling the destructor of A but object A is still there When the scope of the object gets over it automatically calls the destructor again. Hence we get A twice
8th Jun 2018, 11:21 AM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar