I know this is pretty common question, but still new for me!
I don\'t understand concept of dangling pointer, was googling around, and writing test methods to find o
As a matter of style, I explain a dangling pointer as "a pointer which still exists, even though the object it pointed to no longer exists".
In your case, the pointer name
exists for a shorter period that the object that it points to. So it's never dangling.
Inside common C++ classes, pointers dangle for a very short period, inside destructors. That's because the delete
statement is before the last }
of the destructor, while the pointer itself ceases to exist at the last }
. If you don't want to worry about this, use e.g. unique_ptr
. The T*
pointer will dangle for a very short time inside the unique_ptr::~unique_ptr
destructor, which is perfectly safe.