请问一下大家,我这段代码哪里不对呢
程序代码:
#include <iostream> class hstring { private: unsigned short usmlen; //新建的内存长度 char* cstr; //字符串 unsigned short uslen; //字符串长度 unsigned short getlength(const char* str) ; //得到对象的字符串长度 void copystr(char* dest, const char* source); //复制内存函数 public: hstring(const hstring& str); hstring(const char* _str); hstring& operator= (const hstring& str); char* getstr() { return cstr; } hstring(); }; hstring::hstring() { usmlen = 0x32; uslen = 0; cstr = new char[usmlen]; } unsigned short hstring::getlength(const char* str) { unsigned short len{}; for (; str[len];len++) return len; } void hstring::copystr(char* dest, const char* source) { unsigned short len = getlength(source); if (len > usmlen) { cstr = new char[len]; //重新分配内存 usmlen = len; } memcpy(dest, source, len); } hstring::hstring(const char* _str) { copystr(cstr,_str); } hstring::hstring(const hstring& str) { copystr(cstr, str.cstr); } hstring& hstring::operator=(const hstring& str) { copystr(cstr, str.cstr); return *this; } int main() { char str[]{ "1234567890aaaaa" }; hstring hstr(str); hstring hstr1=hstr; std::cout << hstr.getstr()<<std::endl; std::cout <<hstr1.getstr(); system("pause"); }
为什么我这里的代码跑起来就崩溃了呢?是哪里错了呢?清各位高手解答