#include <iostream>
using namespace std;
namespace GB
{
class Test
{
public:
Test() { cout << "Constructor is executed\n"; }
~Test() {
cout << i << " " << "Destructor is executed\n";
this->i = 7;
}
int i = -1;
};
}
int main()
{
// Test(); // Explicit call to constructor
GB::Test t; // local object
t.i = 6;
t.~Test(); // Explicit call to destructor
return 0;
}
Output
Constructor is executed
6 Destructor is executed
6 Destructor is executed
My questions are:
1)Why destructor is called twice.
2)In first call of destructor memeber value is changed from 6 to 7 , still in second call it comes as 6.
3)Can we stop second call of destructor (I want to keep only manually call of destructor).