数据结构系列均是本人个人搜集和写出来的,若有错误请大家批评指正。(有的是c++,有的是伪代码)
1. String class String { public: String( const char * str = NULL ); String( const String & other); ~String(); String & operator=( const String & other); private: char * m_p; }; String::String( const char * str ) { if ( NULL == str ) { m_p = new char[1]; if ( NULL != m_p ) { *m_p = '\0'; } } else { m_p = new char[strlen(str)+1]; if ( NULL != m_p ) { strcpy( m_p, str ); } } } String::String( const String & other ) { m_p = new char[strlen(other.m_p)+1]; if ( NULL != m_p ) { strcpy( m_p, other.m_p ); } } String & String::operator=( const String & other ) { if ( this != &other ) { delete [] m_p; m_p = new char[strlen(other.m_p)+1]; if ( NULL != m_p ) { strcpy( m_p, other.m_p ); } } return *this; } String::~String() { delete [] m_p; }
|
|