title: SIMPLE string implementation in c++
body: <p>I started writing a very simple implementation of a string class in c++, here is the code:</p>
  
  <pre><code>class String
  {
  public:
      String()
      {
          this-&gt;_length = 0;
          this-&gt;_size = 0;
          this-&gt;_string = NULL;
      }
      String(const char* str)
      {
          this-&gt;_length = strlen(str);
          this-&gt;_size = this-&gt;_length + 1;
          this-&gt;_string = new char[this-&gt;_size];
  
          strcpy_s(this-&gt;_string, this-&gt;_size, str);
      }
      ~String()
      {
          if (this-&gt;_string != NULL)
          {
              delete[] this-&gt;_string;
              this-&gt;_string = NULL;
          }
      }
  
      String&amp; operator+(const char* str)
      {
          String* temp = new String();
  
          temp-&gt;_length = strlen(str) + strlen(this-&gt;_string);
          temp-&gt;_size = temp-&gt;_length + 1;
          temp-&gt;_string = new char[temp-&gt;_size];
  
          strcpy_s(temp-&gt;_string, temp-&gt;_size, this-&gt;_string);
          strcat_s(temp-&gt;_string, temp-&gt;_size, str);
  
          return (String&amp;)*temp;
      }
  
      int Length()
      {
          return this-&gt;_length;
      }
  
  private:
      int _size;
      int _length;
      char* _string;
  };
  </code></pre>
  
  <p>You can see that my implementation of operator+ is absolutely wrong, in fact there is a memory leak.
  Writing the operator+= is way simpler because I can simply concatenate the char* with this->_string and return *this.
  I need help with the operator+ implementation.</p>
  
  <p>Note: This is homework so I don't want the solution the copy-paste but it would be awesome if someone could point me in the right direction...</p>
  
  <p>Thanks</p>
  
  <p>Edit:
  I added the copy constructor:</p>
  
  <pre><code>String(const String&amp; str)
  {
      this-&gt;_length = str._length;
      this-&gt;_size = str._size;
      this-&gt;_string = new char[this-&gt;_size];
      strcpy_s(this-&gt;_string, this-&gt;_size, str._string);
  }
  </code></pre>
  
  <p>the operator= and the operator+=:</p>
  
  <pre><code>String&amp; operator=(const String&amp; str)
  {
      if (this != &amp;str)
      {
          this-&gt;_length = str._length;
          this-&gt;_size = str._size;
          this-&gt;_string = new char[this-&gt;_size];
  
          strcpy_s(this-&gt;_string, this-&gt;_size, str._string);
      }
  
      return *this;
  }
  String&amp; operator+=(const String&amp; str)
  {
      this-&gt;_length = this-&gt;_length + str._length;
      this-&gt;_size = this-&gt;_length + 1;
  
      char* buffer = new char[this-&gt;_size];
      strcpy_s(buffer, this-&gt;_size, this-&gt;_string);
      strcat_s(buffer, this-&gt;_size, str._string);
  
      delete[] this-&gt;_string;
      this-&gt;_string = buffer;
  
      return *this;
  }
  </code></pre>
  
  <p>but there is still something wrong because if I run a while(true) loop like this:</p>
  
  <pre><code>while (true)
  {
      String a = String("string a");
      String b = a;
      b = "string b";
      b += " string c";
  }
  </code></pre>
  
  <p>the memory used by the process will increase continuously</p>