I tried fiddling around with the unique_ptr.
Why does the line marked with MK1 (comment) not crash my program?
Foo destructor is called. By using unique_ptr I thought both the destructor for the object was called and deleted the memory allocated for the object itself.
I'm guessing its unique, so I shouldnt let others point to the same Foo?
void ExMemory() {
Foo *simplePtr;
{
std::unique_ptr<Foo> p1(new Foo);
simplePtr = &*p1; // Does unique_ptr increment a reference counter?
p1->bar();
simplePtr->bar();
}
simplePtr->bar(); // MK1: Why can I do this? Foo destructor is called.
delete simplePtr;
simplePtr->bar(); // Causes the error I expected earlier.
}
int main() {
cout << "Hello main" << endl;
ExMemory();
int x;
cin >> x;
return 0;
}