Write my own string class is my assignment and there have some problems i can't figure out why?
here is my code:
//this is one kind of constructor--this one i couldn't change;
String::String(char ch, size_t count /* = 1 */)
{
memset(m_buffer,ch,count);
m_length=count;
}
//this is overload operator
String &String::operator = (const String& other)
{
if(this->m_buffer!=other.m_buffer)
{
if(m_buffer!=NULL)
{
delete [] m_buffer;
m_length=strlen(other.m_buffer) + 1;
m_buffer = new char[m_length];
strcpy(m_buffer,other.m_buffer);
}
else
{
m_length=strlen(other.m_buffer) + 1;
m_buffer = new char[m_length];
strcpy(m_buffer,other.m_buffer);
}
return *this;
}
else
return *this;
}
String& String::operator = (const char *other)
{
if(this->m_buffer!=other)
{
if(m_buffer!= NULL)
{
delete [] m_buffer;
m_length=strlen(other)+1;
m_buffer=new char[m_length];
strcpy(m_buffer,other);
}
else
{
m_length=strlen(other)+1;
m_buffer=new char[m_length];
strcpy(m_buffer,other);
}
return *this;
}
else
return *this;
}
and the driver:
int main()
{
String ditty;
ditty = String('p',0);
}
couldn't pass
if somebody know how to solve this problem, plz help me.