title: Overloading + operator in String class
body: <p>I have to implement a string class with operators [], +, +=, &lt;&lt;, >>, ==, !=, &lt;, &lt;=, >, >= but I cannot use the string object or include the  library for c++. Here is my code so far: </p>
  
  <pre><code># define _CRT_SECURE_NO_WARNINGS
  #include&lt;iostream&gt;
  #include&lt;cstring&gt;
  #include&lt;fstream&gt;
  using namespace std;
  class MyString {
  public:
      MyString();
      MyString(const char* chars);
      int length() const;
      char * getValue()const;
      ~MyString();
      //copy constructor
      MyString(const MyString&amp;);
      MyString&amp; operator =(const MyString&amp; s);
      char&amp; operator[](int i);
      MyString&amp; operator+=(const MyString&amp; s);
      friend ostream&amp; operator&lt;&lt;(ostream&amp;os, const MyString&amp;s);
      friend istream&amp; operator &gt;&gt; (istream&amp;is, MyString&amp;s);
  
      friend bool operator ==(const MyString&amp; s1, const MyString&amp; s2);
      friend bool operator &lt;(const MyString&amp; s1, const MyString&amp; s2);
  
  private:
      char* value;
      int size;
  };
  MyString operator+(const MyString s1, const MyString&amp; s2);
  bool operator !=(const MyString&amp; s1, const MyString&amp; s2);
  bool operator &gt;=(const MyString&amp; s1, const MyString&amp; s2);
  bool operator &lt;=(const MyString&amp; s1, const MyString&amp; s2);
  bool operator &gt; (const MyString&amp; s1, const MyString&amp; s2);
  //no arguments constructor
  MyString::MyString() {
      value = 0;
      size = 0;
  }
  //constructor
  MyString::MyString(const char* chars) {
      size = strlen(chars);
      value = new char[size + 1];
      strcpy(value, chars);
  }
  int MyString::length()const {
      return size;
  }
  char* MyString::getValue()const {
      return value;
  }
  MyString::~MyString() {
      delete[] value;
  }
  //copy constructor
  MyString::MyString(const MyString&amp; s) {
      size = s.size;
      value = new char[size + 1];
      strcpy(value, s.value);
  }
  MyString&amp; MyString::operator=(const MyString&amp;s) {
      if (s.value == 0) {
          delete[] value;
          value = 0;
          size = 0;
          return *this;
      }
      if (this != &amp;s) {
          delete[] value;
          value = new char[s.size + 1];
          strcpy(value, s.value);
          size = s.size;
      }
      return *this;
  }
  char&amp; MyString::operator[](int i) {
      if (i &lt; 0 || i &gt;= size) {
          cout &lt;&lt; "Please enter correct value of index" &lt;&lt; endl;
          exit(0);
      }
      else {
  
          return value[i];
      }
      return value[i];
  }
  MyString&amp; MyString::operator+=(const MyString&amp; s) {
      char** temp = &amp;value;
      *temp = new char[size];
      strcpy(value,*temp);
      strcat(value, s.getValue());
  
      if (size != 0) {
          delete[] temp;
      }
      size = strlen(value);
      return *this;
  }
  ostream&amp; operator&lt;&lt;(ostream&amp;out, const MyString&amp;s) {
      out &lt;&lt; s.value;
      return out;
  }
  istream&amp; operator &gt;&gt; (istream&amp;in, MyString&amp;s) {
      char*ptr = new char[100];
      in &gt;&gt; ptr;
      s = MyString(ptr);
      delete ptr;
      return in;
  }
  MyString operator+(const MyString s1, const MyString&amp; s2) {
      MyString answer;
      answer += s1;
      answer+= s2;
      return answer;
  }
  bool operator ==(const MyString&amp;s1, const MyString&amp; s2) {
      return(strcmp(s1.value, s2.value) == 0);
  }
  bool operator&lt;(const MyString&amp; s1, const MyString&amp; s2) {
      return (strcmp(s1.value, s2.value) &lt; 0);
  }
  bool operator !=(const MyString&amp; s1, const MyString&amp; s2) {
      return !(s1 == s2);
  }
  bool operator&gt;(const MyString&amp; s1, const MyString&amp; s2) {
      return !(s1 == s2) &amp;&amp; !(s1 &lt; s2);
  }
  bool operator&lt;=(const MyString&amp; s1, const MyString&amp; s2) {
      return s1 &lt; s2 || s1 == s2;
  }
  bool operator &gt;=(const MyString &amp; s1, const MyString&amp; s2) {
      return !(s1 &lt; s2);
  }
  void test_copy_and_destructor(MyString S) {
      cout &lt;&lt; "test: copy constructor and destructor calls: " &lt;&lt; endl;
      MyString temp = S;
      cout &lt;&lt; "temp inside function test_copy_and_destructor: " &lt;&lt; temp &lt;&lt; endl;
  }
  
  int main() {
  
      MyString st1("abc abc");
      MyString st2("9fgth");
  
      cout &lt;&lt; "Copy constructor , &lt;&lt; operator" &lt;&lt; endl;
  
      MyString  st3(st1);
  
      cout &lt;&lt; "st3: " &lt;&lt; st3 &lt;&lt; endl;
  
      test_copy_and_destructor(st2);
  
      MyString  st4;
  
      cout &lt;&lt; "operator + " &lt;&lt; endl;
  
      st4 = st3 + st2;
  
      cout &lt;&lt; "st4: " &lt;&lt; st4 &lt;&lt; endl;
  
      cout &lt;&lt; "st1 + st2: " &lt;&lt; (st1 + st2) &lt;&lt; endl;
  
      cout &lt;&lt; "operators  [ ] " &lt;&lt; endl;
  
      for (int i = 0; i &lt; st2.length(); i++)
      cout &lt;&lt; st2[i] &lt;&lt; " ";
  
      cout &lt;&lt; endl;
  
      cout &lt;&lt; "operators  += , ==, != " &lt;&lt; endl;
  
      st2 += st1;
  
      if (st3 == st1)
      cout &lt;&lt; "st3 and st1 are identical " &lt;&lt; endl;
      else cout &lt;&lt; "st3 and st1 are not identical " &lt;&lt; endl;
  
      if (st2 != st1)
      cout &lt;&lt; "st2 and st1 are not identical " &lt;&lt; endl;
      else cout &lt;&lt; "st2 and st1 are identical " &lt;&lt; endl;
  
      cout &lt;&lt; "operators  &lt; , &lt;=, &gt;, &gt;= " &lt;&lt; endl;
  
      if (st2 &lt; st1)
      cout &lt;&lt; "st2 &lt; st1 " &lt;&lt; endl;
      else cout &lt;&lt; "st2 is not less than st1 " &lt;&lt; endl;
  
      if (st1 &lt;= st2)
      cout &lt;&lt; "st1 &lt;= st2 " &lt;&lt; endl;
      else cout &lt;&lt; "st1 is not less than or equal to st2 " &lt;&lt; endl;
  
      if (st1 &gt; st2)
      cout &lt;&lt; "st1 &gt; st2 " &lt;&lt; endl;
      else cout &lt;&lt; "not (st1 &gt;  st2) " &lt;&lt; endl;
  
      if (st1 &gt;= st2)
      cout &lt;&lt; "st1 &gt;= st2 " &lt;&lt; endl;
      else cout &lt;&lt; "not (st1 &gt;=  st2) " &lt;&lt; endl;
  
      cout &lt;&lt; "operator &gt;&gt; " &lt;&lt; endl;
  
      //Open the data file
      ifstream  input("A9_input.txt");
      if (input.fail()) {
          cout &lt;&lt; "unable to open input file A9_input.txt, Exiting..... ";
      return 0;
      }
      MyString temp1;
      MyString temp2("aaa");
      input &gt;&gt; temp1;
      input &gt;&gt; temp2;
      cout &lt;&lt; "first element of input file: " &lt;&lt; temp1 &lt;&lt; endl;
      cout &lt;&lt; "second element of input file: " &lt;&lt; temp2 &lt;&lt; endl;
      input.close();
  
      cout &lt;&lt; "MyString says farewell....." &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>After reaching operator + my code reaches break point and crashes. I could tell something is wrong with my += operator code as + is using += code. Can someone help me figure out what is wrong with my += code?</p>