目录
注意点(不只是String类所要注意的点,所有的C++类都需要注意的地方)
开发环境
win10+vs2017
注意点(不只是String类所要注意的点,所有的C++类都需要注意的地方)
- 等号赋值的三步走1,防止自己给自己赋值 2,释放之前所占的外部资源 3,申请新的空间然后将数据拷贝进去
- 析构函数执行后记得要把指针变量指向nullptr,避免野指针的出现。举例<如果析构没有在释放内存后将指针指向空>
str在第二次调用析构函数后delete一个野指针程序必崩。String str1; str.~String(); str.~String();
- 在String类中写参数为const String&等号"="运算符函数的时候记得将返回定义为String,这么做的目的是可以实现这样的连等操作
String str1 String str2 String str3 str1=str2=str3
实现代码
#include<iostream>
using namespace std;
enum error_num {OUTOFRANDGE};
//1,保证string没有空(不然每次操作的时候都需要判断是否为空,很麻烦)
//2,不要出现野指针
class String
{
public:
String(const char*ptr=nullptr)
{
//保证String类中_ptr不为空指针
if (ptr != nullptr)
{
_ptr = new char[strlen(ptr) + 1];
strcpy(_ptr, ptr);
}
else
{
_ptr = new char[1];
_ptr = '\0';
}
}
~String() { delete[]_ptr; _ptr = nullptr; }
String(const String &src)
{
_ptr = new char[strlen(src._ptr) + 1];
strcpy(_ptr, src._ptr);
}
String(String &&src)
{
_ptr = src._ptr;
src._ptr = nullptr;//++++++++++++++很重要很重要很重要很重要
}
//返回值为String目的是为了可以连等于操作
String& operator=(const String &src)
{
if (&src == this)
return *this;
delete[]this->_ptr;
_ptr = new char[strlen(src._ptr) + 1];
strcpy(_ptr, src._ptr);
return *this;
}
String operator=(String &&src)
{
delete[]this->_ptr;//首先释放自己占有的外部资源,不然容易引起内存泄漏
_ptr = src._ptr;
src._ptr = nullptr;
}
//等号右边的操作数为const char*不存在连等赋值,所以返回值为void
void operator=(const char*_str)
{
delete[]this->_ptr;
if (_str == nullptr)
{
_ptr = new char[1];
_ptr = '\0';
}
else
{
_ptr = new char[strlen(_str) + 1];
strcpy(_ptr, _str);
}
}
bool operator>(const String&str)const
{
return strcmp(_ptr, str._ptr) > 0;
}
bool operator<(const String &str)const
{
return strcmp(_ptr, str._ptr) < 0;
}
bool operator==(const String &str)const
{
return strcmp(_ptr, str._ptr) == 0;
}
int length()const
{
return strlen(_ptr);
}
const char*c_str()const
{
return _ptr;
}
char &operator[](int index)
{
if (index > length() - 1 || index < 0)
throw OUTOFRANDGE;
return _ptr[index];
}
private:
char *_ptr;
friend String operator + (const String &lhs, const String &rhs);
friend ostream& operator<<(ostream &out, const String &str);
friend istream& operator>>(istream &in, String &str);
};
String operator+ (const String &lhs, const String &rhs)
{
String tmp;
tmp._ptr = new char[strlen(lhs._ptr) + strlen(rhs._ptr) + 1];
strcpy(tmp._ptr, lhs._ptr);
strcat(tmp._ptr, rhs._ptr);
return tmp;
}
ostream& operator<<(ostream &out, const String &str)
{
out << str._ptr;
return out;
}
istream& operator>>(istream &in, String &str)
{
/*delete[]str._ptr;
str._ptr = nullptr;
char tmp[1024] = { 0 };
in >> tmp;
str = tmp;
return in;*/
delete[] str._ptr;
str._ptr = new char[1024];
in >> str._ptr;
return in;
}
相关的测试代码
int main()
{
String str1 = "aaa";
String str2 = "bbb";
String str3 = str1 + str2;
String str4 = str1 + "ccc";
String str5 = "ddd" + str4;
cout << "str5:" << str5 << endl;
cin >> str1; // cin >> scanf
cout << "str1:" << str1 << endl;
// bool operator>(const MyString &str)
if (str5 > str1)
{
cout << "str5 > str1" << endl;
}
int size = str1.length(); // string str1
for (int i = 0; i < size; ++i)
{
cout << str1[i]; // ar & operator[](int index)
}
cout << endl;
// string => char buf[1024]
char buf[1024] = { 0 };
strcpy(buf, str1.c_str());
cout << "buf:" << buf << endl;
return 0;
cout << str5;
return 0;
}