#include <iostream>using namespace std;class mstring
{public:
mstring(const char* str)
{
length = strlen(str);
m_char = new char(length + 1);
strcpy_s(m_char, length + 1, const_cast<char*>(str));
} friend ostream& operator<<(ostream& out,const mstring& str)
{ if (NULL != str.m_char)
{
out << str.m_char; return out;
}
}
~mstring()
{ if (m_char != NULL)
{ delete m_char;
length = 0;
}
} char* m_char; int length;
};int main(){
mstring* mstr = new mstring("Hello World!"); cout << mstr; return 0;
}`为什么内存会出错???
- 2 回答
- 0 关注
- 768 浏览
添加回答
举报
0/150
提交
取消