转帖:关于string类的两种实现

来源:互联网 发布:高尔夫视频分析软件 编辑:程序博客网 时间:2024/05/22 00:46

林锐的C/C++高质量编程里面的实现

String 类的原型如下

class String
{
   public:
          String(const char *str=NULL); //
构造函数
          String(const String &other); //
拷贝构造函数
          ~String(void); //
析构函数
          String& operator=(const String &other); //
等号操作符重载

        ShowString();

   private:
          char *m_data; //
指针
};


String::~String()
{
    delete [] m_data; //
析构函数,释放地址空间
}


String::String(const char *str)
{
    if (str == NULL)//
当初始化串不存在的时候,为m_data申请一个空间存放'/0'
     {
        m_data = new char[1]
        *m_data = '/0';
     }
    else//
当初始化串存在的时候,为m_data申请同样大小的空间存放该串;
     {
        int length = strlen(str);
        m_data = new char[length + 1];
        strcpy(m_data, str);
     }
}


String::String(const String &other)//
拷贝构造函数,功能与构造函数类似。
{
    int length = strlen(other.m_data);
    m_data = new char[length + 1];
    strcpy(m_data, other.m_data);
}


String& String::operator =(const String &other)
{
    if (this == &other)//
当地址相同时,直接返回;
        return *this;

    delete [] m_data;//
当地址不相同时,删除原来申请的空间,重新开始构造;

    int length = strlen(other.m_data);
    m_data = new char[length+1];
    strcpy(m_data, other.m_data);

    return *this;
}

 

String::ShowString()//由于m_data是私有成员,对象只能通过public成员函数来访问;

{

      cout<<this->m_data<<endl;

}

 

main()
{
      String AD;
      char * p="ABCDE";
      String B(p);
      AD.ShowString();
      AD=B;
      AD.ShowString();
}

 

C++ Primer里面的实现?

#include<iostream>

using namespace std;

 

class String

{

friend ostream& operator<< (ostream&,String&);

public:

    String(const char* str=NULL);    //赋值构造兼默认构造函数(char)

    String(const String &other);           //赋值构造函数(String)

    String& operator=(const String&other);       //operator=

    String operator+(const String &other)const; //operator+

    bool operator==(const String&);              //operator==

    char& operator[](unsigned int);              //operator[]

    size_t size(){return strlen(m_data);}

~String(void) {delete[] m_data;}

 

private:

    char *m_data;

};

 

inline String::String(const char* str)

{

    if (!str) m_data=0;

    else

    {

        m_data = new char[strlen(str)+1];

        strcpy(m_data, str);

    }

}

 

inline String::String(const String& other)

{

    if(!other.m_data) m_data=0;

    else

    {

        m_data=new char[strlen(other.m_data)+1];

        strcpy(m_data,other.m_data);

    }

}

 

inline String& String::operator=(const String& other)

{

    if (this!=&other)

    {

        delete[] m_data;

        if(!other.m_data) m_data=0;

        else

        {

            m_data = new char[strlen(other.m_data)+1];

            strcpy(m_data,other.m_data);

        }

    }

    return *this;

}

 

inline String String::operator+(const String &other)const

{

    String newString;

    if(!other.m_data)

        newString = *this;

    else if(!m_data)

        newString = other;

    else

    {

        newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+1];

        strcpy(newString.m_data,m_data);

        strcat(newString.m_data,other.m_data);

    }

    return newString; //不能返回局部变量的指针或是引用

}

 

inline bool String::operator==(const String &s)    

{

    if ( strlen(s.m_data) != strlen(m_data) )

        return false;

    return strcmp(m_data,s.m_data)?false:true;

}

 

inline char& String::operator[](unsigned int e)

{

    if (e>=0&&e<=strlen(m_data))

        return m_data[e];

}

 

ostream& operator<<(ostream& os,String& str)

{

    os << str.m_data;

    return os;

}

void main()

{

    String str1="Hello!";

    String str2="Teacher!";

    String str3 = str1+str2;

    cout<<str3<<"/n"<<str3.size()<<endl;

}

http://hi.baidu.com/marktian/blog/item/916f1a17f8e35100c83d6db4.html