class String{
private:
char* m_str;
public:
String(const char* str=NULL);
~String();
String(const String& other);
void print_str(void){
cout << "m_String:" << m_str << endl;
}
String& operator = (const String &other);
};
String::String(const char* str){
if (str==NULL){
m_str = new char[1];
*m_str = '\0';
}
else
{
m_str = new char[strlen(str) + 1];
strcpy(m_str,str);
}
}
String::~String(void){
if (m_str!=nullptr){
delete[]m_str;
m_str = nullptr;
}
}
String::String(const String& other){
if (other.m_str==NULL){
m_str = new char[1];
*m_str = '\0';
}
else{
m_str = new char[strlen(other.m_str) + 1];
strcpy(m_str, other.m_str);
}
}
String& String::operator = (const String &other){
if (this==&other){
return *this;
}
delete[]m_str;
m_str = new char[strlen(other.m_str) + 1];
strcpy(m_str,other.m_str);
return *this;
}
int _tmain(int argc, _TCHAR* argv[])
{
String a("hello world");
a.print_str();
cout << "---------------" << endl;
String c(a);
c.print_str();
String b = a;
b.print_str();
c = a;
c.print_str();
system("pause");
return 0;
}