03_模仿string

来源:互联网 发布:js函数没有返回值 编辑:程序博客网 时间:2024/06/07 22:46

模仿string函数,写出构造、析构、赋值、拷贝函数

class  men{    public:        men( const char *str=NULL ){            if( NULL==str ) {                ptr = new char[1] ;                ptr[0] = '\0' ;            }else{                int len = strlen(str) ;                ptr = new char[len+1] ;                strcpy( ptr , str ) ;            }        }        ~men( void ){            delete []ptr ;        }        men( const men& other ) {            int len = strlen( other.ptr ) ;            ptr = new char[len+1] ;            strcpy( this->ptr , other.ptr ) ;        }        men& operator=( const men& other ) {            if( this==other ) {                return *this ;            }            delete []ptr ;            int len = strlen(other.ptr) ;            ptr = new char[len+1] ;            strcpy( ptr , other.ptr ) ;            return *this ;        }    private:        char *ptr ;};
原创粉丝点击