首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

string 结构 拷贝 和析构函数

2013-10-08 
string 构造 拷贝 和析构函数已知String的原型为class String{public:String(const char *str NULL)//普

string 构造 拷贝 和析构函数
已知String的原型为
class String
{
public:
String(const char *str =NULL);//普通构造函数
String(const String &other);//拷贝构造函数
~String(void);//析构函数
String &operate=(const String &other);//赋值函数
private:
char *m_date;//用于保存字符串
};
0

  • 回复
    • 1楼
    • 2011-03-09 13:03
      • 举报 |
        • string 结构 拷贝 和析构函数
        • hufeng3405871
        • 中级粉丝2/*普通构造函数*/
          String(const char *str = NULL)
          {
          if(str == NULL)
          {
              m_date=new char[1];
             *m_date='/0';
          }
          else
          {
          int Long=strlen(str);
          this->m_date=new char[Long+1];
          strcpy(m_dat,str);
          }
          }
          回复
          • 2楼
          • 2011-03-09 13:10
            • 举报 |
              • string 结构 拷贝 和析构函数
              • hufeng3405871
              • 中级粉丝2/*拷贝构造函数*/
                String(const String &other)
                {
                int Long=strlen(other.m_date);
                m_date=new char[Long+1];
                strcpy(m_date,other.m_date); 
                }
                回复
                • 3楼
                • 2011-03-09 13:15
                  • 举报 |
                    • string 结构 拷贝 和析构函数
                    • hufeng3405871
                    • 中级粉丝2/*构造函数*/
                      ~String(void)
                      {
                      delete []m_date;
                      }
                      回复
                      • 4楼
                      • 2011-03-09 13:16
                        • 举报 |
                          • string 结构 拷贝 和析构函数
                          • hufeng3405871
                          • 中级粉丝2/*赋值函数*/
                            String operate(const String &other)
                            {
                            if(this == &other)
                            {
                                 return *this;
                                 delete [] m_date;
                               }
                               else
                            {
                                 int Long=strlen(other.m_date);
                                 m_date=new char[Long+1];
                                strcpy(m_date,other.m_date);
                                  return   *this;
                               }
                            }

热点排行