zoukankan      html  css  js  c++  java
  • 实现类的构造函数,析构函数和赋值函数

    已知类String 的原型为:

    class String

    {

     public:

     String(const char *str=NULL);//普通构造函数

     String(const String &other);//拷贝构造函数

     ~String(void);

     String & operate=(const String &other);//赋值函数

    private:

     char *m_data;//用于保存字符串

    };

    参考:http://blog.csdn.net/zhuimengzh/article/details/6708882

    #include<iostream>
    using namespace std;
    class String
    {
    
     public:
    
     String(const char *str=NULL);//普通构造函数
    
     String(const String &other);//拷贝构造函数
    
     ~String(void);
    
     String & operator=(const String &other);//赋值函数
    
    private:
    
     char *m_data;//用于保存字符串
    
    };
    
    String::~String(void)//析构函数
    {
        delete []m_data;
    }
    String::String(const char *str)//构造函数
    {
        if(str==NULL)
        {
          m_data=new char[1];
          if(m_data==NULL)
          {
              //检查内存申请是否成功
              std::cout<<"申请内存失败!"<<std::endl;
              exit(1);
          }
          m_data='';
        }
        else
        {
            m_data=new char[strlen(str)+1];
            if(m_data==NULL)
            {
              //检查内存申请是否成功
              std::cout<<"申请内存失败!"<<std::endl;
              exit(1);
            }
            strcpy(m_data,str);
        }
    }
    String::String(const String &str)//输入参数为const,为什么要为const
    {
        if(str.m_data==NULL)
        {
          m_data=new char[1];
          if(m_data==NULL)
          {
              //检查内存申请是否成功
              std::cout<<"申请内存失败!"<<std::endl;
              exit(1);
          }
          m_data='';
        }
        else
        {
            m_data=new char[strlen(str.m_data)+1];
            if(m_data==NULL)
            {
              //检查内存申请是否成功
              std::cout<<"申请内存失败!"<<std::endl;
              exit(1);
            }
            strcpy(m_data,str.m_data);
        }
    }
    String& String::operator=(const String &str)
    {
        if(this==&str)//检查自赋值
            return *this;
        delete [] m_data;
        m_data=new char[strlen(str.m_data)+1];
        if(m_data==NULL)
        {
              //检查内存申请是否成功
              std::cout<<"申请内存失败!"<<std::endl;
              exit(1);
        }
        strcpy(m_data,str.m_data);
        return *this;
    }
    int main()
    {
        String a;
        String b("abc");
        a=b;
        printf("%s",b);
        printf("%s",b);
        return 0;
    }

    解释:拷贝函数和赋值函数中const的作用。

    1),String s1("hello"); Const String s2("wj"); s1=s2;

    这里会出错,因为一个const变量是不能随意转化为非const变量的。

    2)  String s1("hello"); String s2("wj");String s3;

    s3=s1+s2; “+”赋值必须返回一个操作值已知的String对象,除非它是一个const对象。

    作者:wj704    出处:http://www.cnblogs.com/wj204/   
  • 相关阅读:
    周赛F题 POJ 1458(最长公共子序列)
    HDU 4720 Naive and Silly Muggles 2013年四川省赛题
    HDU 4716 A Computer Graphics Problem 2013年四川省赛题
    SCU 4440 Rectangle 2015年四川省赛题
    SCU 4436 Easy Math 2015年四川省赛题
    大数模板——六种实现了加减乘除和求余
    HDU 1002 A + B Problem II
    CodeForces 689C  Mike and Chocolate Thieves
    CodeForces 689A -Mike and Cellphone
    CodeForces 595B
  • 原文地址:https://www.cnblogs.com/wj204/p/3349463.html
Copyright © 2011-2022 走看看