C++ String类的简单实现1

来源:互联网 发布:手机淘宝总无响应 编辑:程序博客网 时间:2024/05/04 18:57
一、String类的基本设计: 
 String类的最简单的设计是只含有一个char*成员变量。
<span style="white-space: pre;"></span><pre name="code" class="cpp">        String(char * str = ""):_str(new char [sizeof(str) + 1]){strcpy(_str, str);}

 二、实现最基本的构造、析构、拷贝构造、赋值几种最基本成员函数:
  1、构造函数:<span style="font-family: Arial, Helvetica, sans-serif;">成员变量为私有的,要对其进行初始化,必须用一个共有成员函数来进行。同时这个函数应该有且仅有在定义对象时自动执行一次(构造函数不可以显示调用)</span>
   <pre name="code" class="cpp">String(char * str = ""):_str(new char [sizeof(str) + 1]){strcpy(_str, str);}<span style="font-family: Arial, Helvetica, sans-serif;">   </span>
   2、析构函数:当一个对象生命周期结束时,C++编译系统会自动调用的一种成员函数。
<span style="white-space:pre"></span><pre name="code" class="cpp">~String(){if (_str != NULL){delete[] _str;_str = NULL;}}
3、拷贝构造函数:创建对象时使用同类对象来进行初始化,这是使用的构造函数成为拷贝构造函数;拷贝构造函数是特殊的构造函数。
<pre name="code" class="cpp">      传统写法:
<span style="white-space:pre"></span><pre name="code" class="cpp">String(const String &str)                 :_str(new char[(sizeof(str._str) + 1)]){strcpy(_str, str._str);}
       经过改进之后的写法:<span style="font-family: Arial, Helvetica, sans-serif;"></span>
<span style="white-space:pre"></span><pre name="code" class="cpp">        String(const String &str):_str(NULL){String temp(str._str);swap(_str,temp._str);}
4、赋值函数:该函数属于预算福重载函数中的一类(operator=)
<span style="white-space:pre"></span>传统写法:<pre name="code" class="cpp"><span style="white-space:pre"></span>String& operator=(const String& str){        if ( this != &str){delete[] _str;_str = new char[sizeof(str._str) + 1];strcpy(_str, str._str);      }<span style="white-space:pre"></span>}
<span style="white-space:pre"></span>改进后的写法:<span style="font-family: Arial, Helvetica, sans-serif;"></span>
<span style="white-space:pre"></span><pre name="code" class="cpp">String& operator=(String str){swap(_str, str._str);return *this;}
     5、其他的几个常用的函数的实现:
<pre name="code" class="cpp">     <span>public</span>:
<span style="white-space:pre"></span><pre name="code" class="cpp">        bool operator==(const String & s){if (SameStr(_str,s._str))return true;elsereturn false;}bool operator>(const String & s){if (Compare(_str, s._str))return true;elsereturn false;}bool operator<(const String & s){return ((_str > s._str));}<span style="font-family: Arial, Helvetica, sans-serif;"></span>
<span style="white-space:pre"></span><pre name="code" class="cpp">bool SameStr(const char* s1, const char* s2){assert(s1);assert(s2);while ((*s1) == (*s2)){s1++;s2++;if ((*s1 == '\0') && (*s2 == '\0'))return  true;}  return false;}bool Compare(const char *s1, const char *s2){assert(s1);assert(s2);int ret;while (*s1++ == *s2++)    ret = *s1 - *s2;if (ret > 0)    return true;else    return false;}



三、总结:
     1、为了保证代码的一致性应该尽量是new type[](delete [])只出现在构造(析构)函数中,这样写方便以后对代码的维护工作。
     2、在写operator=()的传统写法时,一定要注意内存泄露的问题,并且需要对自幅值的处理。
     3、在写operator=()的改进写法时,应该注意在初始化列表中对_str的初始化,否则程序将会面临崩溃的危险。
     4、应该注意对库函数的使用。<span style="white-space:pre"></span>

<span style="white-space:pre"></span>
                                             
0 0