#include <cstdio>
#include <cstdlib>
#include <sstream>
class String
{
public:
String(const char * s = NULL);
String(const String& other);
String & operator=(const String & other);
~String();
private:
int len;
char * data;
};
String::String(const char* s)
{
if (s == NULL)
{
data = new char[1];
data = '\0';
}
else
{
int len = strlen(s) + 1;
data = new char[len];
strcpy_s(data,len, s);
}
}
String::String(const String& other)
{
int len = strlen(other.data) + 1;
data = new char[len];
strcpy_s(data,len, other.data);
}
String& String::operator=(const String & other)
{
if (this == &other)
{
return *this;
}
delete[] data;
int len = strlen(other.data) + 1;
data = new char[len];
strcpy_s(data,len, other.data);
return *this;
}
String::~String()
{
if (data != NULL)
{
delete []data;
data = NULL;
}
}
int main()
{
//普通构造
String a("Hello");
String b("World");
//拷贝构造
String c(b);//打印"World"
//赋值
c = a;//打印"Hello"
return 0;
}