MyString関数のプロトタイプを与えて、自分で構造関数を書き出して、構造関数を複製して、構造関数を解析して与えます

1376 ワード

関数プロトタイプ
class MyString
{
 public:
     MyString (const char *str=NULL);
     MyString (const MyString &other);
     ~ MyString(void);
     MyString & operator = (const MyString & other);
 private:
     char *m_data;
};
補完後の関数
//    
class MyString{
public:
    MyString(const char* str = NULL);//    
    MyString(const MyString& other);//      
    ~MyString(void);//    
    MyString& operator=(const MyStirng& other);//    
private:
    char* m_data;
};
//    
MyString :: MyString(const char* str){
    if(str == NULL)
    {
        m_data = new char[1];
        *m_data = '\0';
    }
    else
    {
        int length = strlen(str);
        m_data = new char[length + 1];
        strcpy(m_data, str);
    }
}
//      
MyString :: MyString(const MyString& other){
    int length = strlen(other.m_data);
    m_data = new char[length + 1];
    strcpy(m_data, other.m_data);
}
//    
MyString :: ~MyString(){
    delete[]m_data;
    m_data = NULL;
}
//    
MyString& MyString :: operator=(const MyString& other){
    if(&other == this)
    {
        return *this;
    }
    else
    {
        delete[]m_data;
        m_data = NULL;
        int length = strlen(other.m_data);
        m_data - new char[length + 1];
        strcpy(m_data, other.m_data);
        return *this;
    }
}