title: crash happens while re-initializing a string object
body: <p>Request you to have a deep look at the string class program present below</p>
  
  <pre><code>class String{
  private: int len;
           int size;
           char *p;
  public:  
       int getlen()
       {
           return len;
       }
       int getsize()
       {
           return size;
       }
       char* getp()
       {
           return p;
       }
       String(char *p1=0)
       {
           cout&lt;&lt;"constructor called"&lt;&lt;endl;
           if(p1!=0)
           {
              len=strlen(p1);
              size=len+1;
              p=new char(sizeof(char)*(size));
              strncpy(p,p1,size);
           }
           else
           {
               len=size=0;
               p="";
           }
       }
       String(String &amp;s1)
       {
           cout&lt;&lt;"copy constructor called"&lt;&lt;endl;
           if(&amp;s1!=this)
           {
               cout&lt;&lt;"copy constructor called"&lt;&lt;endl;
               len=s1.getlen();
               size=len+1;
               p=new char(sizeof(char)*(size));
               //char *d=s1.getp();
               strncpy(p,s1.getp(),size);
           }
       }
       void display()
       {
           cout&lt;&lt;"string len="&lt;&lt;len&lt;&lt;endl;
           cout&lt;&lt;"string size="&lt;&lt;size&lt;&lt;endl;
           cout&lt;&lt;"string name="&lt;&lt;p&lt;&lt;endl;
       }
       ~String()
       {
           cout&lt;&lt;"destructor called"&lt;&lt;endl;
           if(strlen(p) &gt;= 1)
           {
              cout&lt;&lt;"111"&lt;&lt;endl;
              delete []p;
           }
           //cout&lt;&lt;"destructor finished"&lt;&lt;endl;
       }
  };
  int main(int argc, char *argv[])
  {
    String s1;
    String s2("hello");
    s1.display();
    s2.display();
    s2=String("Hello World");
    s2.display();
    return 0;
  }
  </code></pre>
  
  <p>Please look at the re-initialization line</p>
  
  <pre><code>     s2=String("Hello World");
  </code></pre>
  
  <p><strong>String("Hello World") will create a temporary String object and the it should call assignment operator. Since default assignment operator by compiler would do shallow copy, after the temporary String object goes out of scope the char pointer present in the String class would become NULL.</strong></p>
  
  <p>I tried to overload the assignment operator as below</p>
  
  <pre><code> String&amp; operator =(String &amp;s1)
   {
       cout&lt;&lt;"operator = called"&lt;&lt;endl;
       if(&amp;s1!=this)
       {
           len=s1.len;
           size=s1.size;
           if(strlen(p)&gt;=1)
                delete p;
           p=new char(sizeof(char)*size);
           strncpy(p,s1.getp(),size);
       }
       return *this;
    }
  </code></pre>
  
  <p>But it is giving error <strong>no match for ‘operator=’ (operand types are ‘String’ and ‘String’)
    s2=String("World");</strong></p>
  
  <p>Request you to compile the program after correcting 'operator=', because I doubt copy constructor might give any error..</p>
  
  <p>Thanks in advance..</p>