String类
代码:
#include <iostream>
#include <cstring>
class String
{
char* m_str;
public:
String(const char* str)
{
if(NULL == str)
{
m_str = new char[1];
*m_str = '\0';
}
else
{
m_str = new char[strlen(str)+1];
strcpy(m_str, str);
}
}
String(const String& that)
{
m_str = new char[strlen(that.m_str)+1];
strcpy(m_str, that.m_str);
}
String& operator = (const String& that)
{
if(&that == this)
{
return *this;
}
delete[] m_str;
m_str = new char[strlen(that.m_str)+1];
strcpy(m_str, that.m_str);
return *this;
}
~String(void)
{
delete[] m_str;
}
};