class String
{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operator =(const String &other); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
//constructor
String::String(const char *str = NULL)
{
if(str == NULL)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
m_data = new char[strlen(str) + 1];
memcpy(m_data, str, strlen(str) + 1);
}
}
//copy constructor
String::String(const String &other)
{
m_data = new char[strlen(other.m_data) + 1];
if(m_data == NULL)
{
std::cerr << "allocator memory failed in String::String(const String &other)";
}
else
{
strcpy(m_data, other.m_data);
}
}
//destructor
String::~String(void)
{
delete [] m_data;
}
//copy-assign operator
String &String::operator=(const String &other)
{
if(this == &other)//self assignment
return *this;
size_t len = strlen(other.m_data);
delete [] m_data;
m_data = new char[len+1];
strcpy(m_data, other.m_data);
return *this;
}
//普通构造函数 String::String(const char *str) { if(str==NULL) { m_data = new char[1]; // 得分点:对空字符串自动申请存放结束标志'\0'的空 //加分点:对m_data加NULL 判断 *m_data = '\0'; } else { int length = strlen(str); m_data = new char[length+1]; // 若能加 NULL 判断则更好 strcpy(m_data, str); } } // String的析构函数 String::~String(void) { delete [] m_data; // 或delete m_data; } //拷贝构造函数 String::String(const String &other) // 得分点:输入参数为const型 { int length = strlen(other.m_data); m_data = new char[length+1]; //加分点:对m_data加NULL 判断 strcpy(m_data, other.m_data); } //赋值函数 String & String::operate =(const String &other) // 得分点:输入参数为const型 { if(this == &other) //得分点:检查自赋值 return *this; delete [] m_data; //得分点:释放原有的内存资源 int length = strlen( other.m_data ); m_data = new char[length+1]; //加分点:对m_data加NULL 判断 strcpy( m_data, other.m_data ); return *this; //得分点:返回本对象的引用 }
#include <iostream> #include <assert.h> using namespace std; class String { public: String(const char* str = NULL); String(const String &other); ~String(); String &operator= (const String &other); private: char *m_data; }; String::String(const char* str) { if (str == NULL) { m_data = new char[1]; assert(m_data != NULL); m_data = '\0'; } else { int len = strlen(str); m_data = new char[len]; assert(m_data != NULL); //memcpy(m_data, str, len); // 法1 strcpy(m_data, str); } } String::String(const String &other) { assert(this != &other); int len = strlen(other.m_data); m_data = new char[len]; assert(m_data != NULL); strcpy(m_data, other.m_data); } String::~String(void) { delete[] m_data; m_data = NULL; } String& String::operator=(const String &other) { if (this == &other) { return *this; } delete[] m_data; m_data = NULL; int len = strlen(other.m_data); m_data = new char[len]; assert(m_data != NULL); memcpy(m_data, other.m_data, len); // 法2:strcpy(m_data, other.m_data); return *this; } int main(char argc, char *argv) { String s1; String s2("222"); s1 = s2; return 0; }
String::String(const char* str){ if(str==NULL) { m_data=new char[1]; *m_data='\0'; } else { int len=strlen(str)+1; m_data=new char[len]; strcpy(m_data,str); } } String::~String(){ delete []m_data; } String::String(const String &other){ m_data=new char[strlen(other.m_data)+1]; strcpy(m_data,other.m_data); } String & String::operator=(const String &other){ if(this==other) { return *this; } delete []m_data; m_data=new char[strlen(other.m_data)+1]; strcpy(m_data,other.m_data); return *this; }
#include <string.h> #include <stdlib.h> #include <assert.h> #define NULL 0 class string { public: string(char* str = NULL); string(const string& str); ~string(); string operator=(const string& str); private: char* m_data; }; string::string(char* str) { if(str == NULL) m_data = NULL; else { m_data = (char*)malloc(strlen(str) + 1); assert(m_data != NULL); strcpy(m_data, str); } } string::~string() { free(m_data); m_data = NULL; } string::string(const string& str) { this->operator=(str); } string string::operator=(const string& str) { if(this == &str) return *this; char* tmp = (char*)malloc(strlen(str.m_data) + 1); assert(tmp != NULL); free(m_data); m_data = tmp; strcpy(m_data, str.m_data); return *this; }
} private: char* m_data; };