I have the following code:
class A
{
public:
int x = 6;
~A() { std::cout << "\ndestr invoked\n"; }
};
int main()
{
int* x;
{
A a;
std::cout << &a.x << "\n";
x = &a.x;
}
std::cout << x << ": " << *x;
}
With output:
0x78cac859fc00
destr invoked
0x78cac859fc00: 6
If I understand correctly it seems that destructor was invoked automatically but in memory variable remained. Does anyone know why?
In the following example (pointer was used so object was deleted manually):
class A
{
public:
int x = 6;
~A() { std::cout << "\ndestr invoked\n"; }
};
int main()
{
int* x;
{
A* a = new A;
std::cout << &a->x << "\n";
x = &a->x;
delete a;
}
std::cout << x << ": " << *x;
}
Variable was cleared:
Output:
0x1381360
destr invoked
0x1381360: 0
Does anyone know what the difference is?