C++ – SIMPLE string implementation in c++

c++string

I started writing a very simple implementation of a string class in c++, here is the code:

class String
{
public:
    String()
    {
        this->_length = 0;
        this->_size = 0;
        this->_string = NULL;
    }
    String(const char* str)
    {
        this->_length = strlen(str);
        this->_size = this->_length + 1;
        this->_string = new char[this->_size];

        strcpy_s(this->_string, this->_size, str);
    }
    ~String()
    {
        if (this->_string != NULL)
        {
            delete[] this->_string;
            this->_string = NULL;
        }
    }

    String& operator+(const char* str)
    {
        String* temp = new String();

        temp->_length = strlen(str) + strlen(this->_string);
        temp->_size = temp->_length + 1;
        temp->_string = new char[temp->_size];

        strcpy_s(temp->_string, temp->_size, this->_string);
        strcat_s(temp->_string, temp->_size, str);

        return (String&)*temp;
    }

    int Length()
    {
        return this->_length;
    }

private:
    int _size;
    int _length;
    char* _string;
};

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.

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…

Thanks

Edit:
I added the copy constructor:

String(const String& str)
{
    this->_length = str._length;
    this->_size = str._size;
    this->_string = new char[this->_size];
    strcpy_s(this->_string, this->_size, str._string);
}

the operator= and the operator+=:

String& operator=(const String& str)
{
    if (this != &str)
    {
        this->_length = str._length;
        this->_size = str._size;
        this->_string = new char[this->_size];

        strcpy_s(this->_string, this->_size, str._string);
    }

    return *this;
}
String& operator+=(const String& str)
{
    this->_length = this->_length + str._length;
    this->_size = this->_length + 1;

    char* buffer = new char[this->_size];
    strcpy_s(buffer, this->_size, this->_string);
    strcat_s(buffer, this->_size, str._string);

    delete[] this->_string;
    this->_string = buffer;

    return *this;
}

but there is still something wrong because if I run a while(true) loop like this:

while (true)
{
    String a = String("string a");
    String b = a;
    b = "string b";
    b += " string c";
}

the memory used by the process will increase continuously

Best Solution

You could reuse the operator+= in the operator+:

(The code below assumes that you have an operator+=, a copy constructor and an assignment operator which is NOT the case in the code you pasted).

EDIT: As suggested by Jerry Coffin the following operator should NOT be a class member but a free operator:

EDIT2: And to allow the compiler a bit more optimizations the first argument is not a const-reference anymore:

String operator+(String a, String const &b) {
  a += b;
  return a;
}

By this you can have a more simple operator+= and simply copy constructor and build the complex thing on the simple ones.

And do not forget:

You MUST implement a copy constructor and assignment operator. Otherwise the compiler generates it for you in a wrong way: The compiler generates code that simply copies the content. So it also copies the pointer but does not allocate new memory for the copy. Then you have two instances referencing the same memory and both try to deallocate it in the destructor which is undefined behavior.