#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 關注
- 802 瀏覽
添加回答
舉報
0/150
提交
取消