Believe that people who have written C + + must have used:
String str2 ("11");
string str1 = str2;
Now let me dig deep into the string str1 = str2, what it contains.
First Floor:
Copied the contents of str2 "11" to str1.
Second floor:
The STR1 object is defined first, and the Str2 object is assigned to STR1
Third floor:
Why the string type can be assigned, the original string implements the assignment operator overload.
- string & string::operate= (const string &other)
- {  
- if ( this == &other)
- return * this
- delete m_data;
- m_data = new char [ Strlen (Other.data) +1];
- strcpy (m_data, other.data);
- return * this // returns a reference to *this without copying the process   
- }
Fourth floor:
Understood to string str1 = str2; The pseudo-code can be written as:
String str1;
str1.= (STR2)
The process of STR1=STR2 is analyzed as follows:
1. Create temporary variable tmp for string&
2. Calling the assignment operator overload function
3, the STR2, str1 in turn to press the stack, the execution function
4, the return value initializes the TMP
5. Destroy TMP
Fifth Floor:
I don't understand:
- String & string::operate= (const string &other)
This overloaded function does not need to return a value at all, why return it!
Please help the god to explain!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + string copy of traced dig bottom! Look at the layers that you understand.