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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384088.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

POJ 3370 Halloween treats——鸽巢原理+思维

【题目描述】 POJ 3370 Halloween treats Description Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a chi…

将信号量代码生成静态库以及动态库

1.信号量相关代码生成静态库 2.信号量相关代码生成动态库

Wormholes——Bellman-Ford判断负环

【题目描述】 While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of…

C++11 标准新特性:Defaulted 和 Deleted 函数

https://www.ibm.com/developerworks/cn/aix/library/1212_lufang_c11new/index.html Defaulted 函数 背景问题 C 的类有四类特殊成员函数&#xff0c;它们分别是&#xff1a;默认构造函数、析构函数、拷贝构造函数以及拷贝赋值运算符。这些类的特殊成员函数负责创建、初始化、…

顺序表实现栈相关操作

1.栈的相关概念 栈是一种特殊的线性表, 其中只允许在固定的一端进行插入和删除元素.进行数据插入和删除的一端叫做栈顶, 另一端成为栈底. 不含任何元素的栈称为空栈, 栈又称为先进先出的线性表. 2. 顺序栈的结构 3. 顺序栈的具体操作 (1). 数据结构 typedef char SeqStackTyp…

MPI Maelstrom——Dijkstra

【题目描述】 BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee’s research advisor, Jack Swigert, has asked her to bench…

双向带环带头结点的链表实现栈

1. 数据结构 利用带头结点带环的结点实现栈的相关操作.因此, 每一个结点包括了一个前驱, 一个后继, 还有一个数据成员 typedef char DLinkStackType;typedef struct DLinkStack {DLinkStackType data;struct DLinkStack* next;struct DLinkStack* prev; }DLinkStack;2. 初始化…

Cow Contest——Floyed+连通性判断

【题目描述】 N (1 ≤ N ≤ 100) cows, conveniently numbered 1…N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors. The contest …

C++11 标准新特性:委派构造函数

https://www.ibm.com/developerworks/cn/rational/1508_chenjing_c11/index.html陈 晶2015 年 8 月 11 日发布WeiboGoogle用电子邮件发送本页面 1本文首先介绍了在委派构造函数提出之前类成员构造所面临的问题&#xff0c;再结合实例介绍了委派构造函数的用法&#xff0c;并说明…

顺序表实现队列

一. 队列相关概念 队列是只允许在一段进行插入元素, 在另一端进行删除元素的线性表,即只允许对队列进行尾插,头删的操作.队列具有先进先出, 后进后出的特性.          1.初始化 void SeqQueInit(SeqQue* q) {if(q NULL){return;//非法输入}q -> head 0;q -> …

Arbitrage——判断正环Bellman-Ford/SPFA

【题目描述】 Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French …

链表实现队列

上篇博客是用顺序表实现队列, 现在用双向带头结点带环链表实现对队列的出队列, 入队列, 取队首元素, 以及销毁队列的相关操作 1.初始化链表 void DLinkQueInit(DLinkQue** q) {if(q NULL){return;//非法输入}if(*q NULL){return;//非法输入带头结点的链表至少有一个傀儡结点…

HDU - 1796——容斥原理+二进制枚举

【题目描述】 Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N12, and M-integer set is {2,3}, so there is another set {2,3,4,…

数据结构学习(二)——单链表的操作之头插法和尾插法创建链表

http://blog.csdn.net/abclixu123/article/details/8210109 链表也是线性表的一种&#xff0c;与顺序表不同的是&#xff0c;它在内存中不是连续存放的。在C语言中&#xff0c;链表是通过指针相关实现的。而单链表是链表的其中一种&#xff0c;关于单链表就是其节点中有数据域和…

信号的基本概念以及信号的产生

一. 信号产生的场景 1. 用户输入命令, 在shell 启动一个前台进程      2. 当用户按一下 Ctrl C 的时候,从键盘产生一个硬件中断      3. 此时CPU 正在执行这个进程的带代码, 则该进程的执行代码暂停执行, CPU 从用户态切换到内核态处理该硬件中断.      4. 中断…

HDU - 1028——母函数入门

【题目描述】 “Well, it seems the first problem is too easy. I will let you know how foolish you are later.” feng5166 says. “The second problem is, given an positive integer N, we define an equation like this: Na[1]a[2]a[3]…a[m]; a[i]>0,1<m<N;…

信号的阻塞

一. 阻塞信号 1.信号的相关概念     (1) 递达: 实际执行信号的处理动作称为信号的递达     (2) 未决: 信号从产生到递达之间的过程叫做信号的未决     (3) 阻塞: 进程可以选择阻塞某个信号, 被阻塞的信号产生时将保持在未决状态, 直到进程解除该信号的屏蔽, 才…

POJ 1511 Invitation Cards——Dijkstra优先队列优化+反向建图

【题目描述】 In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the…

链栈 尹成

http://blog.csdn.net/itcastcpp/article/details/39271661 今天&#xff0c;我们一起用C写链栈&#xff0c;具体如下。 LinkStack.h具体内容&#xff1a; [cpp] view plaincopy #include "StackNode.h" template<typename Type> class LinkStack{ publ…

信号的捕捉以及SIGCHLD信号

一. 信号的捕捉定义 用户提供一个处理函数, 要求内核在处理信号时必须切换到用户态,执行这个函数, 这种方式就叫做信号的捕捉 二. 图解信号的捕捉过程 1. 由上图可以看出,当处理信号的执行动作时用户自定义的时候,此时就需返回该函数去调用该函数, 这就叫做信号的捕捉. 以前我…