重写String类

classs String
{
    private:
    char *m_data;   //数据成员
 
    public:
    String(const char *str = NULL)    //普通构造函数
    {
         if (str == NULL)
         {
             m_data = new char[1];
             *m_data = '\0';
         }
         else
         {
             int len = strlen(str);
             m_data = new char[len+1];
             strcpy(m_data, str);
         }
    }
 
    ~String()    //析构函数
    {
         delete []m_data; 
         m_data = NULL;
    }  
 
    String(const String &other)    //拷贝构造函数
    {
         int len = strlen(other.m_data);
         m_data = new char[len + 1];
         strcpy(m_data, other.m_data);
    }
 
    String& operate=(const String &other)    //赋值函数
    {
         if (&other == this)
         {
              return *this;
         }
         else
         {
              delete []m_data;
              m_data = NULL;
              int len = strlen(other.m_data);
              m_data  = new char[len+1];
              strcpy(m_data, other.m_data);
              return *this;
         }
    }
};

你可能感兴趣的:(二次全面复习)