通过user_string类,来看如何撰写构造函数。
#include #include class user_string{public : user_string(); user_string(const char *src); user_string(const user_string &other); user_string &operator = (const user_string &other); ~user_string(); void show();private: char *m_pdata;};user_string::user_string(){}user_string::user_string(const user_string &other){}user_string::user_string(const char*src){ if ( src == NULL ) { m_pdata = new char[1]; m_pdata[0] = '#include #include class user_string{public : user_string(); user_string(const char *src); user_string(const user_string &other); user_string &operator = (const user_string &other); ~user_string(); void show();private: char *m_pdata;};user_string::user_string(){}user_string::user_string(const user_string &other){}user_string::user_string(const char*src){ if ( src == NULL ) { m_pdata = new char[1]; m_pdata[0] = '\0'; } else m_pdata = new char [strlen(src) + 1]; strcpy(m_pdata, src);}user_string::~user_string(){ delete []m_pdata;}user_string & user_string::operator = (const user_string &other){ if ( this == &other ) return *this; delete []m_pdata; m_pdata = new char[strlen(other.m_pdata) + 1]; strcpy(m_pdata, other.m_pdata); return * this;} void user_string::show(){ printf("%s", m_pdata);}void main(){ user_string a("123"); user_string b("456"); b = b; b = a; //b = a; }
'; } else m_pdata = new char [strlen(src) + 1]; strcpy(m_pdata, src);}user_string::~user_string(){ delete []m_pdata;}user_string & user_string::operator = (const user_string &other){ if ( this == &other ) return *this; delete []m_pdata; m_pdata = new char[strlen(other.m_pdata) + 1]; strcpy(m_pdata, other.m_pdata); return * this;} void user_string::show(){ printf("%s", m_pdata);}void main(){ user_string a("123"); user_string b("456"); b = b; b = a; //b = a; }
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~