#include <iostream>
using namespace std;
class String
{
public:
String():str(NULL)
{
}
~String()
{
if(str)
delete[] str;
}
const char * c_str()
{
return str;
}
char * operator= (const char *s);
String& operator= (const String &s);
private:
char *str;
};
char * String::operator=(const char *s)
{
if(str)
delete[] str;
if(s)
{
str = new char[strlen(s)+1];
strcpy(str,s);
}
else
{
str = NULL;
}
return str;
}
String& String::operator= (const String &s)
{
// if it is self-copy
if(str == s.str)
return *this;
if(str)
delete[] str;
if(s.str)
{
str = new char[strlen(s.str)+1];
strcpy(str,s.str);
}
else
{
str = NULL;
}
return *this;
}
int main()
{
String s;
s = "Good luck,";
cout << s.c_str() << endl;
s = "Shenzhou 8";
cout << s.c_str() << endl;
return 0;
}