String类的构造函数,析构函数、拷贝构造函数和赋值函数

(1)构造函数
String::String(const char *str)
{
if(str==NULL)
{
  m_data  = new char[1];
  *m_data = ‘\0’; 
}
else
{
   Int length = strlen(str);
   m_data = new char[length];
   Strcpy(m_data,str);
}
}
(2)析构函数
String::~String(void)
{
   delete []m_data;
}
(3)拷贝构造函数
  String::String(const String &other)
{
Int length = strlen(other);
 m_data = new char[length+1];
Strcpy(m_data,other.m_data);
}
(4)赋值函数
String & String::operate =  (const String &other) 
 {
     If(this == &other)
       Return *this;
     Delete []m_data;
    m_data = NULL;
    M_data = new char[strlen(other.m_data)];
    Strcpy(m_data,other.m_data);
     Return *this;
}

  

posted @ 2019-07-26 08:55  轻轻的吻  阅读(1448)  评论(0编辑  收藏  举报