Here is the code which basically implementing the = assignment for a class named CMyString
, and the code is right.
CMyString& CMyString::operator =(const CMyString &str) {
if(this == &str)
return *this;
delete []m_pData;
m_pData = NULL;
m_pData = new char[strlen(str.m_pData) + 1];
strcpy(m_pData, str.m_pData);
return *this;
}
The instance is passed by reference, and the first 'if' is checking whether the instance passed in is itself or not. My question is: why does it use &str
to compare, doesn't str
already contain the address of the instance? Could any one explain how this line works?
Also, I just want to make sure that this
contains the address of the object: Is this correct?