Title Requirements:
The Declaration of class cmystring is as follows:
class cmystring{public: cmystring (char *pdata=NULL); CMyString (const cmystring &str); ~cmystring (void); &operator= (const cmystring &str); Private : Char *m_pdata;};
Implement the overloaded function of its assignment operator, which requires exception security, that is, if an exception occurs when an object is assigned, the state of the object cannot be changed.
Reference sword refers to the 1th question of offer.
Topic Analysis:
To be an exception, the state of the object does not change, we first create a temporary instance, then swap the temporary instance and the original instance.
CMyString &cmystring::operator= (const cmystring &str) { if( this!=&str) { cmystring strtemp (str); char *p = strtemp.m_pdata; = m_pdata; = p; }}
C + + exception-safe assignment operator overloading "Microsoft interview 100 question 55th"