I am having this problem where I am trying to overload the add function for my custom string class, so I make a temporary object to add both of the objects lengths and arrays together, however I keep getting segmentation faults and I don't know why. I have tested both my assignment operator works and my equality operator and they are both perfectly working.
myString myString::operator+(const myString& str) const{
int p = 0;
myString tmp;
tmp.sLength = sLength + str.sLength;
tmp.s = new char[tmp.sLength];
while (p != (sLength - 1))
{
tmp.s[p] = s[p];
p++;
}
while (p != (tmp.sLength - 1))
{
tmp.s[p] = str.s[(p - sLength)];
p++;
}
return tmp;
//tmp.s = NULL;
}
myString& myString::operator=(const myString& str)
{
if (this != &str)
{
if ( s != NULL)
{
if (str.s == NULL)
{
sLength = 0;
s = NULL;
}
else
{
delete [] s;
s = new char [str.sLength];
sLength = str.sLength;
for (int i = 0; i < sLength; i++)
s[i] = str.s[i];
}
}
}
return *this;
}