A, Big three (copy constructor, copy assignment, destructor) (video7)
Big three refers to the three specific functions, which are copy constructor, destructor, and copy assignment.
When configured to be copied, copy assignment, destructor:
When the data type is a pointer, for example stored in a string the string class using char *, then it can not be used directly to the default of the compiler Big three. Because the default function byte is copied, the copy of such an object after the pointer points and the object to be copied as such is not a true copy.
class MyString { public : // Common constructors MyString ( const char * CHR = 0 ); // copy constructor MyString ( const MyString & MSTR); // destructor ~ MyString (); // copy assignment MyString & operator = ( const MyString & MSTR) { // detect whether assignment to self IF ( the this == & MSTR) { return * the this ; } // first release existing space, otherwise it will be a memory leak in the Delete [] MyChar; // create a new space, the size and points mstr.mychar space as large MyChar = new new char [strlen (mstr.mychar) + 1 ]; // assign content strcpy (MyChar, mstr.mychar); // returns the left side of the equal sign mystring objects to prevent continuous assignment return * the this ; } Private : // variable is a pointer, copy must be implemented and configured copy assignment char * MyChar; }; MyString :: MyString inline ( const char * chr) { // determine whether the incoming pointer is null (or blank by default) IF (chr) { // if not empty, press chr size distribution of space, and let mychar pointing to the space mychar = new new char [strlen (chr) + . 1 ]; // copy the data into the space chr mychar pointed strcpy (mychar, chr); // if the pointer is null } the else { // create a the size of the space 1 MyChar = new new char [ 1 ]; // save only a \ 0 * MyChar = ' \ 0 '; } } MyString :: MyString inline ( const MyString & MSTR) { // assigned as large a space and mstr.mychar string, and so pointing to the space mychar mychar = new new char [strlen (mstr.mychar) + . 1 ]; // will copy the contents into the space mychar pointed strcpy (mychar, mstr.mychar); } MyString :: inline ~ MyString () { // When the object life cycle to an end, you must clear the memory (heap space), otherwise a memory leak the Delete [] MyChar; }
In the above code, what copy constructor deep copy is actually made, and the default copy constructor do is shallow copy (copy only mstr.mychar 4byte pointer to the mychar). As shown below:
Code, the following part is very important:
// detect whether assignment to self IF ( the this == & MSTR) { return * the this ; }
Mstr and if this is the same object, then if no self-assignment, may cause the program error.
Because before we copy the data, the first step is to delete [] mychar, it is deleted space mstr.mychar points.
The second step we have to refer mstr.mychar point to the size of the space to open up space will be a problem, let alone copy its contents.
So, self-assignment detection is very important.
Second, add class << overloaded functions as mystring
// definition of member methods, obtaining mychar pointer does not modify data, add const inline char * MyString :: get_c_str () const { return the this -> mychar; } // overloaded operator << << mystring_obj so directly COUT; inline the ostream & operator << (OS the ostream &, const MyString & MSTR) { os << mstr.get_c_str(); return os; }
three,