I am reading C++ design pattern and derivative pricing. Some code is like this
(DataPtr is a pointer data number of that class)
Why we have to check the following code and delete DataPtr first before assign it to new address?
if(DataPtr != 0) delete DataPtr;
Code:
Wrapper& operator=(const Wrapper<T>& original)
{
if(this != &original)
{
if(DataPtr != 0)
delete DataPtr;
DataPtr = (original.DataPtr != 0)? original.DataPtr->clone() : 0;
}
return *this;
}
Why we have to check the following code and delete DataPtr first before assign it to new address?
if(DataPtr != 0) delete DataPtr;