构造函数
下面不太完美,if(m_data)delete [] m_data;if(m_data==NULL)return *this;
- class String
- {
- public:
- String(const char *str=NULL);
- String(const String& other);
- String& operator=(const String& other);
- ~String();
- private:
- char *m_data;
- };
- String::String(const char* str)
- {
- if(str==NULL)
- {
- m_data=new char[1];
- *m_data='\0';
- }
- else
- {
- int length=strlen(sizeof(str));
- m_data=new char[length+1];
- strcpy(m_data, str);
- }
- }
- String::String(const String &other)
- {
- int length=strlen(other.m_data);
- m_data=new char[length+1];
- strcpy(m_data, other.m_data);
- }
- 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];
- strcpy(m_data, other.m_data);
- return *this;
- }
- ~String()
- {
- delete []m_data;
- }