#include "stdio.h" #include "string.h" class CMyStr{ public: ~CMyStr(){ if (NULL != m_pData){ delete m_pData; } } CMyStr(const char* pData){ if (NULL == pData){ printf("null data\n"); return; } const int length = strlen(pData); m_pData = new char[length]; if (NULL == m_pData){ throw 1; } memcpy(m_pData, pData, length); } CMyStr(const CMyStr& str){ const int length = strlen(str.m_pData); m_pData = new char[length]; if (NULL == m_pData){ throw 1; } memcpy(m_pData, str.m_pData, length); } CMyStr& operator = (const CMyStr& str){ if (this == &str){ printf("equal\n"); return *this; } else { CMyStr temp(str); char* pData = temp.m_pData; temp.m_pData = m_pData; m_pData = pData; } return *this; } void Print(){ printf("%s\n", m_pData); } private: char* m_pData; }; int main(){ CMyStr test("wee"); test.Print(); CMyStr test1("tes"); test1.Print(); test1 = test; test1.Print(); }
重写字符串类吧
最新推荐文章于 2023-06-25 13:23:54 发布