模仿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 ;
};