Is it defined behavior that I can access a destroyed object from a pointer inside its destructor? Is the object guaranteed to still be at that pointer location and accessible? For example, in the code below, I destroy a1, and inside its destructor I access a1 from a2, which was pointing to a1.
#include <iostream>
class A
{
public:
A(int* i) : m_i(i) {}
~A();
int* m_i;
};
A* a1;
A* a2;
int x = 0;
A::~A()
{
*a2->m_i = 1;
}
int main()
{
a1 = new A(&x);
a2 = a1;
delete a1;
std::cout << x << std::endl;
return 0;
}