#include<iostream>
using namespace std;
class String
{
public:
String(const char* str=NULL); //普通构造函数
String(const String& other); //复制构造函数
~String(); //析构函数
String& operator=(const String& other); //赋值函数
private:
char* m_String;
};
String::~String() //析构函数
{
if(m_String != NULL)
{
delete [] m_String;
m_String = NULL;
}
}
String::String(const char* str) //普通的构造函数
{
if(str==NULL)
{
m_String = new char[1];
*m_String = '\0';
}
else
{
m_String = new char[strlen(str)+1];
strcpy(m_String,str);
}
}
String::String(const String& other) //复制构造函数
{
m_String = new char[strlen(other.m_String+1)];
strcpy(m_String,other.m_String);
}
String& String::operator=(const String& other) //赋值函数
{
if(this == &other)
return *this;
delete [] m_String;
m_String = new char[strlen(other.m_String)+1];
strcpy(m_String,other.m_String);
return *this;
}