Chinaunix首页 | 论坛 | 博客
  • 博客访问: 654119
  • 博文数量: 150
  • 博客积分: 4070
  • 博客等级: 中校
  • 技术积分: 1795
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-23 21:44
文章分类

全部博文(150)

文章存档

2012年(1)

2011年(123)

2010年(26)

分类: C/C++

2011-06-09 20:47:50

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;
         }
    }
};
阅读(3358) | 评论(0) | 转发(0) |
0

上一篇:memcpy与strcpy的区别

下一篇:运算符优先级

给主人留下些什么吧!~~