class MyString{ public:     MyString (const char *str=NULL); //构造函数 ,默认为空      MyString (const MyString &other);//拷贝构造函数      ~ MyString(void);//析构函数      MyString & operator = (const MyString & other);//重载           friend ostream& operator<<(ostream &os, MyString &str);//输出 private:     char *m_data; }; MyString::MyString(const char *str){     if(!str){         m_data = new char[1];         *m_data = '\0';     }     else{         m_data = new char[ strlen(str) + 1 ];         strcpy(m_data,str);     } }  MyString::MyString(const MyString &other){     m_data = new char[ strlen(other.m_data) + 1];     strcpy(m_data,other.m_data);     //cout<<"Copy"<<endl; } MyString::~MyString(){     delete []m_data;     //cout<<"~MyString"<<endl; } MyString& MyString::operator =(const MyString &other){     //cout<<"operator ="<<endl;     if(this != &other){         delete []m_data; //防泄漏          m_data = new char[ strlen(other.m_data) + 1];         strcpy(m_data,other.m_data);     }     return *this; }  ostream& operator<<(ostream &os, MyString &str){     os << str.m_data;     return os; }
点赞 评论

相关推荐

点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务