I am trying to duplicate the string library (to learn it better... and I won't lie, it's for an assignment).

But I am having trouble trying to override the = operator.

Here is what I have:

Code:
mystring& mystring::operator=(const mystring& str){
    
    char *copiedStr;
    
    if (*this == str) return *this;
    
    if (this->size() != str.size())
    {
        delete this;
        copiedStr = new char[str.size()];
        strcpy (copiedStr, str.c_str());
        this = copiedStr;
    }
    
    return *this;
}
I will admit that I do realize that this code is all jacked up. I have been playing with it so much that I don't even know where I originally started, so it's prolly best to just restart. But I'm stubborn.

Here's the idea... (at least, how I think it should work)
We'll call passing string as str1 and the destination (in this case "this") as str2.

If str1 is the same as str2, do nothing
Otherwise copy str1 to str2.

I am assuming that you would have to make sure they are the same size, and I am under the impression that if they are not the same size, the easiest thing to do would be create a new array. Once the array is created, just copy str1 to str2 and return str2.

However, I should be returning *this and I don't know how to change "this" to point to the new array.

The error I am getting now is
Code:
error: lvalue required as left operand of assignment
(referring to "this = copiedStr;")

Can someone please point me in the right direction or show me where I can look to see how the string class is coded? I imagine it's got to be posted somewhere, but I can't find it.