#include<iostream>
#include<cstdio>
#include<assert.h>
#include<cstring>
#include<algorithm>
using namespace std;
class String
{
public:
String(const char* str=NULL);//普通构造函数
String(const String &other);//拷贝构造函数
~String(void);//析构函数
String &operator = (const String &other);//赋值函数
private:
char *m_data;//保存字符串
};
String::String(const char *str)
{
if(str==NULL)
{
m_data=new char[1];//对空字符串自动申请存放结束标志'\0'的空
*m_data='\0';//对m_data加NULL判断
}
else
{
int length=strlen(str);
m_data=new char[length+1];
strcpy(m_data,str);//把str复制到m_data数组里面
}
}
String::~String(void)
{
delete[] m_data;
m_data=nullptr;
}
String::String(const String &other)
{
int length=strlen(other.m_data);
m_data=new char[length+1];
strcpy(m_data,other.m_data);
}
//String::String &operator=(const String &other)
String& String::operator=(const String &other)
{
if(this==&other)//检查自赋值
{
return *this;
}
delete[] m_data;//释放原有的内存
int length=strlen(other.m_data);
m_data=new char[length+1];//对m_data加NULL判断
strcpy(m_data,other.m_data);
return *this;//返回本对象的引用
}
int main()
{
return 0;
}