编写类String的构造函数,拷贝构造函数,析构函数和赋值函数

#include <assert.h> #include <string> class String { public: String(const char* pChar); String(const String& other); ~String(); String & operator=(const String & other); private: char * m_Char; }; //Construct function String::String(const char * pChar) { if( pChar == NULL) { assert((m_Char = new char[1]) != NULL); *m_Char = '/0'; } else { assert((m_Char = new char[strlen(pChar) + 1]) != NULL); strcpy(m_Char, pChar); } } // Copy Construct function String::String(const String& other) { if(other.m_Char == NULL) m_Char = NULL; else { assert((m_Char = new char[strlen(other.m_Char) + 1]) != NULL); strcpy(m_Char, other.m_Char); } } //Destruct function String::~String() { delete [] m_Char; m_Char = NULL; } // operator = function String& String::operator =(const String &other) { assert(this != &other); delete [] m_Char; if(other.m_Char == NULL) m_Char = NULL; else { assert((m_Char = new char[strlen(other.m_Char) + 1]) != NULL); strcpy(m_Char, other.m_Char); } return *this; }

你可能感兴趣的:(String,null,delete,include)