+ 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(); }
1 Antwort
+ 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