I read the following code for deleting pointer object in the open source project X3C.
//! Delete pointer object.
/*!
\ingroup _GROUP_UTILFUNC
\param p pointer object created using 'new'.
*/
template<class T>
void SafeDelete(T*& p)
{
if (p != NULL)
delete p;
p = NULL;
*(&p) = NULL;
}
But I don’t know the meaning of this line:
*(&p) = NULL;
In the above line(p = NULL;), p is assigned to NULL. I think it needs to do that again in another way.
It’s completely pointless. Under ordinary conditions, the unary
*and&operators are inverses of each other (with a few details, like that the expression&*foois not an lvalue even iffoois an lvalue), although operator overloading can change that behavior.However, since
T*is always a pointer type regardless of the typeT, it’s impossible to overload the unaryoperator&forT*, so*(&p)is equivalent to justp, and assigningNULLtopa second time is useless.