<pre name="code" class="cpp">class String
{
private:
char* m_data;
public:
String(const char* str=NULL);
String(const String&);
~String();
String& operator=(const String&);
};
String::String(const char* str)
{
if(str == NULL)//表明是空串
{
m_data = new char[1];
m_data = '\0';
}
else
{
int len = strlen(str);
m_data = new char[len+1];
strcpy_s(m_data, len+1,str);
}
}
String::String(const String& s)
{
int len = strlen(s.m_data);
m_data = new char[len+1];
strcpy_s(m_data, len+1,s.m_data);
}
String::~String()
{
delete[] m_data;
}
String& String::operator=(const String& s)
{
if(this == &s) return *this;
int len = strlen(s.m_data);
char* newchar = new char[len+1];
strcpy_s(newchar, len+1,s.m_data);
delete this->m_data;
m_data = newchar;
return *this;
}
编写String类的构造函数,析构函数,赋值运算符
最新推荐文章于 2018-08-16 15:31:51 发布