The differences between the copy constructor and the value assignment function are as follows:Code, Think about the difference between the two.
The copy constructor is called when an object is defined, as shown in figure
Class string {//...}; string a = B; // B is a string type object or string A (B );
The value assignment function is called when a class instance is assigned to another instance, for example
String A; A = B;
The prototype of the constructor, destructor, and assignment function of the string type is as follows:
Class string {public: string (const char * STR = NULL); // constructor string (const string & other); // copy constructor ~ String (void); // destructor string & operator = (const string & other); // value assignment function PRIVATE: char * m_data; // save string }; // constructorstring: string (const char * Str) {If (STR = NULL) {m_data = new char [1]; m_data [0] = '\ 0 ';} else {int Len = strlen (STR); m_data = new char [Len + 1]; Assert (m_data! = NULL); strcpy (m_data, STR) ;}// copy constructor: Use string: string (const string & Other) when defining a Type Variable) {int length = strlen (Other. m_data); m_data = new char [Length + 1]; Assert (m_data! = NULL); strcpy (m_data, other. m_data);} // destructorstring ::~ String (void) {Delete [] m_data;} // value assignment function: used to assign a class instance to another class instance string & string :: operator = (const string & Other) {If (this = & Other) {return * This;} Delete [] m_data; int length = strlen (Other. m_data); m_data = new char [Length + 1]; Assert (m_data! = NULL); strcpy (m_data, other. m_data); return * This ;}