I am having a hard time getting used to the way C++ handles dynamic and automatic memeory.
My question:
Can a pointer, that points to an automatic allocated instance, keep that instance from deallocation even after the scope of the instanciation has been left?
In this post I read that all pointers pointing to dealloc memory are invalid.
But is this guy talking about the behaviour after a manual dealloc or an automatic dealloc?
This is an example:
int main(){
UiTreeRecord child = UiTreeRecord::UiTreeRecord();
createSomeScope(child);
//Does child still have a valid parent?
//Or does parent point to a piece of memory that has been deallocated?
}
void createSomeScope(const UiTreeRecord& child){
UiTreeRecord root = UiTreeRecord::UiTreeRecord();
child.attachParent(root);
}
void UiTreeRecord::attachParent(UiTreeRecord& newParent) {
if(parent != nullptr) {
detachParent();
}
parent = &newParent;
}