One of the biggest and most confusing-to-beginners misnomer in C++ is the term "deleting a pointer". This has undoubtedly originated from the fact that a delete
expression takes a pointer as its argument:
T * p = new T; // #1
delete p; // #2
However, what's really going on is that line #1 creates a new, dynamic, unnamed object. Think about this again: There is no variable whose value is the object create in line #1. The object is really out of reach, as indeed it does not live in any scope. All we have is a pointer to it.
To end the lifetime of a dynamic variable, we have to use a delete
expression. But since we already know that we can only ever really have a pointer the object, not the object itself*, the expression conveniently accepts a pointer to the object we're deleting.
So really we should say that in line #2 "we are deleting the object *p
by giving a pointer to it to the delete
expression" (namely &*p == p
).
The pointer itself is entirely unaffected by the delete
call.
*) Yes, we could also have a reference variable, like T & r = *new T;
, but that would be insane.