C++实现String类

http://blog.csdn.net/randyjiawenjie/article/details/6709539


C++实现String类,还没有完成,待继续。

有以下注意的点:

(1)赋值操作符返回的是一个MyString&,而重载的+返回的是一个MyString。其中的原因参看《effective c++》,主要是返回引用的时候,必须返回必须在此函数之前存在的引用,因为引用是一个名字,“在我们使用之前,必须想想他代表了那个名字“。如果返回了一个局部对象的引用,那么这个函数结束后,这个引用就会指向一个不存在的对象,显然,这是走上了行为未定义的快车道。

[cpp]  view plain  copy
  1. # include <iostream>  
  2. # include <memory>  
  3. # include <cstring>  
  4. using namespace std;  
  5. class MyString {  
  6. private:  
  7.     char *m_data;  
  8. public:  
  9.     MyString();  
  10.     MyString(const char* ptr);  
  11.     MyString(const MyString& rhs);  
  12.     ~MyString();  
  13.     MyString& operator=(const MyString& rhs);  
  14.     MyString operator+(const MyString& rhs);  
  15.     char operator[](const unsigned int index);  
  16.     bool operator==(const MyString& rhs);  
  17.     friend ostream& operator<<(ostream& output, const MyString &rhs);  
  18. };  
  19. //默认的构造函数  
  20.  MyString::MyString() {  
  21.     m_data = new char[1];  
  22.     *m_data = '\0';  
  23. }  
  24. //使用const char* 来初始化  
  25.  MyString::MyString(const char* ptr) {  
  26.     if (NULL == ptr) {  
  27.         m_data = new char[1];  
  28.         *m_data = '\0';  
  29.     } else {  
  30.         int len = strlen(ptr);  
  31.         m_data = new char[len + 1];  
  32.         strcpy(m_data, ptr);  
  33.     }  
  34. }  
  35. //拷贝构造函数  
  36.  MyString::MyString(const MyString& rhs) {  
  37.     int len = strlen(rhs.m_data);  
  38.     m_data = new char[len + 1];  
  39.     strcpy(m_data, rhs.m_data);  
  40. }  
  41. bool MyString::operator ==(const MyString& rhs) {  
  42.     int result = strcmp(m_data, rhs.m_data);  
  43.     if (0 == result)  
  44.         return true;  
  45.     else  
  46.         return false;  
  47. }  
  48. //赋值操作符  
  49.  MyString& MyString::operator =(const MyString& rhs) {  
  50.     if (this != &rhs) {  
  51.         delete[] m_data;  
  52.         m_data = new char[strlen(rhs.m_data) + 1];  
  53.         strcpy(m_data, rhs.m_data);  
  54.     }  
  55.     return *this;  
  56. }  
  57. //重载运算符+  
  58.  MyString MyString::operator+(const MyString &rhs) {  
  59.     MyString newString;  
  60.     if (!rhs.m_data)  
  61.         newString = *this;  
  62.     else if (!m_data)  
  63.         newString = rhs;  
  64.     else {  
  65.         newString.m_data = new char[strlen(m_data) + strlen(rhs.m_data) + 1];  
  66.         strcpy(newString.m_data, m_data);  
  67.         strcat(newString.m_data, rhs.m_data);  
  68.     }  
  69.     return newString;  
  70. }  
  71. //重载下标运算符  
  72.  char MyString::operator [](const unsigned int index) {  
  73.     return m_data[index];  
  74. }  
  75. //析构函数  
  76.  MyString::~MyString() {  
  77.     delete[] m_data;  
  78. }  
  79. //重载<<  
  80.  ostream& operator<<(ostream& output, const MyString &rhs) {  
  81.     output << rhs.m_data;  
  82.     return output;  
  83. }  
  84. int main() {  
  85.     const char* p = "hello,world";  
  86.     MyString s0 = "hello,world";  
  87.     MyString s1(p);  
  88.     MyString s2 = s1;  
  89.     MyString s3;  
  90.     s3 = s1;  
  91.     MyString s4 = s3 + s1;  
  92.     bool flag(s1 == s2);  
  93.     cout << s0 << endl;  
  94.     cout << s1 << endl;  
  95.     cout << s2 << endl;  
  96.     cout << s3 << endl;  
  97.     cout << flag << endl;  
  98.     char result = s3[1];  
  99.     cout << result << endl;  
  100.     cout << s4 << endl;  
  101.     return 0;  
  102. }  


运行结果:

hello,world
hello,world
hello,world
hello,world
1
e

hello,worldhello,world

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值