
Originally Posted by
Gerald Bates
I am currently trying to eliminate the use of char * within my project in favour of having my own string class.
One thing that your code lacks that I believe has not been mentioned is that the class lacks an assignment operator. Otherwise, you can't write code like this:
Code:
vmString s1="x";
vmString s2;
s2 = s1; // <-- This will cause issues
A simple implementation of the assignment operator is below:
Code:
#include <algorithm>
//...
vmString& vmString::operator=(vmString str)
{
std::swap(str.m_nLength, m_nLength);
std::swap(str.m_pchStr, m_pchStr);
return *this;
}
This code works, provided that your copy constructor and destructor for vmString are working correctly. Basically, we allow the compiler to create a copy by using pass-by-value (the str argument). Then the copy's contents are swapped out with the current contents of this.
Regards,
Paul McKenzie