实现简洁版的string类
#include<iostream>usingnamespcestd;classString{public:String():_str(newchar[1]){_str='\0';}String(constchar*str)//一个字符串的拷贝:_str(newchar[strlen(str)+1]{strcpy(_str,str);}String(constString&str)//一个对象的拷贝:_str(NULL){Stringtmp=str._str;//调用了拷贝构造函数生成了一个String类的对象swap(_str,tmp._str);}/*String(constString&str)//一个对象的拷贝:_str(newchar[strlen(str._str)+1){strcpy(_str,str._str);}*/String&operator=(Stringstr)//直接调用拷贝构造函数生成str{swap(_str,str._str);return*this;}/*String&operator=(constString&str)//书上常用的方法{if(this==&str){return*this;}delete_str;_str=NULL;_str=newchar[strlen(str._str)+1];strcpy(_str,str._str);return*this;}*/private:char*_str;};
以上方法是最简单实现string类的方法,也有其它的方法!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。