首页 > 其他 > 详细

string类默认构造函数

时间:2015-11-23 06:33:54      阅读:304      评论:0      收藏:0      [点我收藏+]
//#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

string类默认构造函数

原文:http://10541556.blog.51cto.com/10531556/1715708

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!