1. The case with pointers--class with pointer member (s) in a class often occurs, typically: the String class.
2. The string class in STL is too complex, copy on write, and so on.
3. Use "Defensive header file declaration".
4. S2 assignment to S3.
String S3 ("hello"= s2;
5. Complier is generated by default: Copy constructors and copy assignment functions (operator overloading), which are executed in a bitwise order. a class with pointers is not suitable for using the default constructor.
// Copy Construction // Copy Assignment
6. The implementation principle of the String class
classstring{ Public: String (Const Char* cstr=0); Private: Char* M_DATA;//The pointer dynamically allocates storage space}string::string (Const Char*CStr) { if(CStr) {m_data=New Char[Strlen (CStr) +1]; strcpy (M_data, CStr); } Else{m_data=New Char[1]; *m_data =' /'; }}
C + + object-oriented Advanced development course (second week)