title: string class assignment operator overloading in c++
body: <p>I defined my own string class, MyString. Everything works well until I assign one object to the other by the overloaded operator=. I know where the problem is but I don't know how to fix it. Any helps?</p>
  
  <pre><code>class MyString{
  public:
      MyString( const MyString *strIni );
      MyString( const char *str);
      ~MyString();
      MyString&amp; operator=( const MyString &amp;str );
  private:
      char *str;
  }
  
  
  MyString::MyString( const MyString *strIni ){
      this-&gt;str = new char[strlen(strIni-&gt;str)+1];
      strcpy(this-&gt;str,strIni-&gt;str) ;
  };
  
  
  MyString::MyString( const char *str){
      this-&gt;str = new char[ strlen(str) + 1 ];
      strcpy(this-&gt;str , str);
  };
  
  MyString::~MyString(){
      delete [] this-&gt;str ;
      cout &lt;&lt; "successfully deleted..." &lt;&lt; endl;
  };
  
  
  MyString&amp; MyString::operator=( const MyString &amp;str ){
      // temp obj holding the rhs
      MyString strTmp(str);
      // temp char pointer holding the rhs
      char *cTmp = strTmp.str;
      // temp obj holding this, later release this memory 
      strTmp.str = this-&gt;str ;
      // this holding rhs; assignment done.
      this-&gt;str = cTmp;
      return *this ;
  };
  
  
  int main(){
      {                                  // line 1
          MyString mystr1("string #1");  // line 2
          MyString mystr2("string #2");  // line 3
          mystr1 = mystr2;               // line 4
      }                                  // line 5
      return 0;
  }
  </code></pre>
  
  <p>The problem of the code is: at line4, after the assignment the pointer in two objects mystr1 and mystr2 both point the same string "string #2". When the program jump out of the brackets at line 5, the destructors are automatically called by sequence: mystr2 and then mystr1. After mystr2 is destructed, the memory of "string #2" has been released. When the destructor of mystr1 is trying to release non-existing memory, the program crashed. </p>
  
  <p>Anybody can help me to fix the overloading member function. When I assign mystr1 = mystr2, i can create a new string instead of making the two pointers pointing the same string.</p>
  
  <p>Thanks a lot!!</p>
  
  <hr>
  
  <hr>
  
  <p>Updates for further questions......
  thank tons!!</p>
  
  <p>actually, i am using copy-and-swap in the overloading function. based on @Mateusz Kołodziejski 's advice, i modified it:</p>
  
  <pre><code>MyString&amp; MyString::operator=( const MyString &amp;rhs ){
      if( this != &amp;rhs ){
          // copy using constructor
          MyString strTmp(rhs) ;
          // swap        
          char *cTmp = strTmp.str;
          // strTmp will be destructed, thus the memory in this will be released
          strTmp.str = this-&gt;str ;
          // size of rhs
          const int str_size = strlen(rhs.str);
          this-&gt;str = new char[str_size+1];
          copy(rhs.str,rhs.str+str_size,this-&gt;str);
      }
      return *this ;
  };
  </code></pre>
  
  <p>when the destructors are called, no crash. But if a printout member function is added, there seems another problem:</p>
  
  <pre><code>void MyString::printout(){
      int str_size = strlen(this-&gt;str);
      cout &lt;&lt; "string size: " &lt;&lt; str_size &lt;&lt; endl ;
      for( int i=0;i&lt;str_size;i++ ){
          cout &lt;&lt; *(this-&gt;str + i);
      }
  }
  </code></pre>
  
  <p>in main function:</p>
  
  <pre><code>int main(){
      {                                  
          MyString mystr1("string #1");  
          MyString mystr2("string #2");  
          mystr1.printout();
          mystr2.printout();
          mystr1 = mystr2;  
          cout &lt;&lt; "after assignment: " &lt;&lt; endl;
          mystr1.printout();
          mystr2.printout();             
      }                                  
  return 0;
  }
  </code></pre>
  
  <p>the results are:</p>
  
  <pre><code>string #1
  string #2
  after assignment...
  string #2═²²²²
  string #2
  </code></pre>
  
  <p>seems that mystr1 is not normal...</p>
  
  <p>anybody can explain this for me?</p>
  
  <p>Thank tons!!</p>