Why behavior like this is allowed?:
class A {
public:
void print() { cout << ++x; }
private:
int x = 10;
};
int main() {
A* ptr;
int* ptr2;
{
A a;
ptr = &a;
int b = 10;
ptr2 = &b;
}
ptr->print();
cout << ++*ptr2;
}
program output: 11 11
Here we are using objects that has already been destroyed. Why program isn't throwing exceptions in this case? Will exception rise the moment program allocate something in place of destroyed objects?