目录
构造
- //构造函数
- String(const char* str = "")
- {
- if (nullptr == str)
- {
- assert(false);
- return;
- }
- _str = new char[strlen(str) + 1];
- strcpy(_str, str);
- }
析构
- //析构函数
- ~String()
- {
- if (_str)
- {
- delete[] _str;
- _str = nullptr;
- }
- }
代码展示:
- #define _CRT_SECURE_NO_WARNINGS
- #include<iostream>
- using namespace std;
- #include<string>
- #include<assert.h>
- //构造
- //拷贝构造
- //赋值运算符重载
- //析构函数
- class String
- {
- public:
- //构造函数
- String(const char* str = "")
- {
- if (nullptr == str)
- {
- assert(false);
- return;
- }
- _str = new char[strlen(str) + 1];
- strcpy(_str, str);
- }
- //析构函数
- ~String()
- {
- if (_str)
- {
- delete[] _str;
- _str = nullptr;
- }
- }
- private:
- char* _str;
- };
- void TestString()
- {
- String s1("hello sunlang!");
- String s2(s1);
- cout << s1 << endl;
- cout << s2 << endl;
- }
- int main()
- {
- TestString();
- return 0;
- }

浅拷贝:编译器合成默认的拷贝构造,导致s1,s2共用同一块内存空间,在释放时同一块空间被释放多次而引起程序崩溃。
深拷贝:每个对象都有一份独立的资源,涉及到资源的管理,其拷贝构造函数、赋值运算符重载以及析构函数必须要显示给出。

- //传统版本写法的String类
- class String
- {
- public:
- //构造
- String(const char* str = "")
- {
- if (nullptr == str)
- {
- assert(false);
- return;
- }
- _str = new char[strlen(str) + 1];
- strcpy(_str, str);
- }
- //拷贝构造
- String(const String& s)
- :_str(new char[strlen(s._str) + 1])
- {
- strcpy(_str, s._str);
- }
- //赋值重载
- String& operator=(const String& s)
- {
- if (this != &s)
- {
- char* pStr = new char[strlen(s._str) + 1];
- strcpy(pStr, s._str);
- delete[] _str;
- _str = pStr;
- }
- return *this;
- }
- //析构
- ~String()
- {
- if (_str)
- {
- delete[] _str;
- _str = nullptr;
- }
- }
- private:
- char* _str;
- };
- #define _CRT_SECURE_NO_WARNINGS
- #include<iostream>
- #include<string>
- #include<assert.h>
- using namespace std;
- class String
- {
- public:
- //构造
- String(const char* str = "")
- {
- if (nullptr == str)
- {
- assert(false);
- return;
- }
- _str = new char[strlen(str) + 1];
- strcpy(_str, str);
- }
- //拷贝构造
- String(const String& s)
- :_str(nullptr)
- {
- String strTmp(s._str);
- swap(_str, strTmp._str);
- }
- //赋值重载
- String& operator=(String s)
- {
- swap(_str, s._str);
- return *this;
- }
- ~String()
- {
- if (_str)
- {
- delete[] _str;
- _str = nullptr;
- }
- }
- private:
- char* _str;
- };
- #define _CRT_SECURE_NO_WARNINGS
- #include<iostream>
- using namespace std;
- #include<assert.h>
- namespace sunlang
- {
- class string
- {
- public:
- typedef char* iterator;
- public:
- //构造
- string(const char* str = "")
- {
- _size = strlen(str);
- _capacity = _size;
- _str = new char[_capacity + 1];
- strcpy(_str, str);
- }
- //拷贝构造
- string(const string& s)
- :_str(nullptr)
- , _size(0)
- , _capacity(0)
- {
- string tmp(s._str);
- this->swap(tmp);
- }
- //赋值重载
- string& operator=(string s)
- {
- this->swap(s);
- return *this;
- }
- //析构
- ~string()
- {
- if (_str)
- {
- delete[] _str;
- _str = nullptr;
- }
- }
- //iterator
- iterator begin()
- {
- return _str;
- }
- iterator end()
- {
- return _str + _size;
- }
- //modify
- void push_back(char c)
- {
- if (_size == _capacity)
- {
- reverse(_capacity * 2);
- }
- _str[_size++] = c;
- _str[_size] = '\0';
- }
- string& operator+=(char c)
- {
- push_back(c);
- return *this;
- }
- void clear()
- {
- _size = 0;
- _str[_size] = '\0';
- }
- void swap(string& s)
- {
- std::swap(_str, s._str);
- std::swap(_size, s._size);
- std::swap(_capacity, s._capacity);
- }
- const char* c_str()const
- {
- return _str;
- }
- //capacity
- size_t size()const
- {
- return _size;
- }
- size_t capacity()const
- {
- return _capacity;
- }
- bool empty()const
- {
- return 0 == _size;
- }
- void resize(size_t newSize, char c = '\0')
- {
- if (newSize > _size)
- {
- if (newSize > _capacity)
- {
- reserve(newSize);
- }
- memset(_str + _size, c, newSize - _size);
- }
- _size = newSize;
- _str[newSize] = '\0';
- }
- void reverse(size_t newCapacity)
- {
- if (newCapacity > _capacity)
- {
- char* str = new char[newCapacity + 1];
- strcpy(str, _str);
- delete[]_str;
- _str = str;
- _capacity = newCapacity;
- }
- }
- //access
- char& operator[](size_t index)
- {
- assert(index < _size);
- return _str[index];
- }
- const char& operator[](size_t index)const
- {
- assert(index < _size);
- return _str[index];
- }
- };
- }