本文共 780 字,大约阅读时间需要 2 分钟。
class CString{public: CString(const char *str = NULL); //普通构造函数 CString(const CString &other); //拷贝构造函数 ~CString(); //析构函数 CString &operator = (const CString &other); //赋值函数private: char *m_data; //用于保存字符串 };CString::CString(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); }}CString::~CString(){ delete[] m_data;}CString::CString(const CString &other){ int lenght = strlen(other.m_data); m_data = new char[lenght+1]; strcpy(m_data, other.m_data);}CString& CString::operator = (const CString &other){ if (this==&other) return *this; delete[] m_data; int length = strlen(other.m_data); m_data = new char[length+1]; strcpy(m_data, other.m_data); return *this;}
转载地址:http://ubkqi.baihongyu.com/