//#include<iostream>
//using namespace std;
//#pragma warning(disable :4996)
//class String
//{
//public:
// /*String()
// :_str(new char[strlen("")+1])
// {
// *_str = ‘\0‘;
// }
// String(const char*s)
// :_str(new char[strlen(s) + 1])
// {
// strcpy(_str, s);
// }*/
// String(const char* s = "")
// :_str(new char[strlen(s) + 1])
// {
// strcpy(_str, s);
// }
// String(const String& s)
// :_str(new char[(strlen(s._str))+1])
// {
// strcpy(_str, s._str);
// }
// String& operator=(const String& s)
// {
// if (this != &s)
// {
// delete[] _str;//重新赋值时需释放原来的空间,否则会内存泄露
// _str = new char[(strlen(_str)) + 1];
// strcpy(_str, s._str);
// }
// return *this;
// }
//
// ~String()
// {
// delete[] _str;
// }
// void Display()
// {
// cout << _str << endl;
// }
//private:
// char* _str;
//};
//void Test1()
//{
// /*String s1;
// s1.Display();
//
// String s2(s1);
// s2.Display();*/
//
// String s1("abcd");
// s1.Display();
// String s2;
// s2.Display();
//
// s2 = s1;
// s2.Display();
//
//
//}
//int main()
//{
// Test1();
// system("pause");
// return 0;
//}要注意内存管理,很容易内存泄漏哦!
本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1715708
原文:http://10541556.blog.51cto.com/10531556/1715708