String类

来源:互联网 发布:氰化钾淘宝自杀 编辑:程序博客网 时间:2024/06/04 18:23

class String
{
 public:
  String(const char *str = NULL); // 普通构造函数
  String(const String &other); // 拷贝构造函数
  ~ String(void); // 析构函数
  String & operate =(const String &other); // 赋值函数
 private:
  char *m_data; // 用于保存字符串
};

 

String::String(const char *str = NULL);

{

    if(NULL == str)

    {

        m_data = new char[1];

        m_data = '/0';

    }

    else

    {

        m_data = new char[strlen(str)+1];

        strcpy(m_data, str);

    }

}

String::String(const String &other)

{

    m_data = new char[strlen(other.m_data)+1];

    strcpy(m_data, other.m_data);

}

String::~ String(void)

{

    delete [] m_data;

}

String & String::operate =(const String &other)

{

    if(this == &other)

    {

         return *this;

    }

   

    delete [] m_data;

    m_data = new char[strlen(other.m_data)+1];

    strcpy(m_data, other.m_data);

    return *this;

}

原创粉丝点击