I'm making my own string class (as an exercise) and I know I need a copy constructor and an overloaded = assignment operator but not sure where to start.
the class is simple and only stores the string e.g.
char* m_string;
I currently have a standard constructor (MyString(const char* str)) which takes a const char* string and then allocates the necessary memory via m_string = new char[numChars + 1] (after counting the number of chars there are in the argument)
Obviously I need to be able to assign one string to another with the assignment operator, but also want to be able to construct a string object from another. i.e.
MyString(const MyString& str)
{
}
In regards to the overload= assignment operator
Do I then get the length of both rhs and lhs in terms of number of characters and then resize the amount of memory depending on whether the rhs is longer or shorter than the lhs?
I don't necessarily want the whole answer as I don't believe you learn anything from just being told the answer but some advice and guidance in the right direction would be appreciated.
Thanks