string类的简要实现
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std; class mystring{
public:
mystring(const char *str=NULL);
mystring(const mystring &other);
~mystring(void);
mystring &operator=(const mystring &other);
mystring &operator+=(const mystring &other); char *getString();
private:
char *m_data;
}; //普通构造函数
mystring::mystring(const char *str){
if(str==NULL){
m_data=new char;
*m_data='\0';
}else{
int lenth=strlen(str);
m_data=new char[lenth+];
strcpy(m_data,str);
}
} mystring::~mystring(void){
delete[]m_data;//m_data是内部数据类型,也可以写成delete m_data
} //拷贝构造函数
mystring::mystring(const mystring &other){
//允许操作other的私有成员m_data???
int lenth=strlen(other.m_data);
m_data=new char [lenth+];
strcpy(m_data,other.m_data); } //赋值函数
mystring &mystring::operator=(const mystring &other){ //1.检测自赋值 处理 a=a的情况
if(this==&other){
return *this;
} //2释放原有的内存
delete []m_data; //3分配新的内存资源,并复制内容
int lenth=strlen(other.m_data);
m_data=new char[lenth+];
strcpy(m_data,other.m_data); //4返回本对象的引用
return *this;
} //赋值函数
mystring &mystring::operator+=(const mystring &other){ int left_lenth=strlen(m_data);
int right_lenth=strlen(other.m_data); //1分配新的内存资源,并复制内容
char *temp_data=new char[left_lenth+right_lenth+];
strcpy(temp_data,m_data);
strcat(temp_data,other.m_data);
delete []m_data;//2释放原有的内存 m_data=temp_data; //3返回本对象的引用
return *this;
} char * mystring::getString(){
return m_data;
} int main()
{
mystring a=mystring("");
mystring b=mystring(a);
mystring c=mystring("");
mystring d;
d=c; a+=d;
printf("%s\n",a.getString()); a+=a;
printf("%s\n",a.getString()); }
注意构造函数,拷贝构造函数,赋值函数,析构函数的写法
重载的写法
参考:高质量C++C 编程指南
string类的简要实现的更多相关文章
- String类的简要概述(1)
String类时我们平时用的比较多的一个类,该类属于java中引用数据类型. String类里面有很多方法需要我们学习.如切割,追加,拼接等. String s = "abcdef" ...
- JAVA集合类简要笔记 - 内部类 包装类 Object类 String类 BigDecimal类 system类
常用类 内部类 成员内部类.静态内部类.局部内部类.匿名内部类 概念:在一个类的内部再定义一个完整的类 特点: 编译之后可生成独立的字节码文件 内部类可直接访问外部类私有成员,而不破坏封装 可为外部类 ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- VC++ 标准C++中的string类的用法总结
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...
- [C++][语言语法]标准C++中的string类的用法总结
转自:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 要想使用标准C++中string类,必须要包含 #include ...
- 标准C++中string类的用法
转自博客园:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非 ...
- 标准C++中的string类的用法总结(转)
http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的 ...
- C++中的string类(2)
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...
- 浅析String类
这是对于String类的一些总结,我将会从几个方面并且结合着字符串池等相关知识进行总结 进程如下: 1.对于String类基本知识的一些总结 2.简要介绍字符串池 3.分 ...
随机推荐
- ubuntu下各类快捷键汇总记录
一.环境 Ubuntu 16.04 二.快捷键汇总如下 2.1 将图形界面下的terminal最大化:ctrl+super+向上的方向键 2.2 将图形界面下的terminal最小化:ctrl+sup ...
- 前端工程化 - npm
什么是npm npm的全称Node Package Manager,npm原先只是作为nodejs的包管理工具,然而随着前端社区的发展,如今npm不仅是nodejs的包管理工具,还是前端js的包管理工 ...
- 利用Minhash和LSH寻找相似的集合(转)
问题背景 给出N个集合,找到相似的集合对,如何实现呢?直观的方法是比较任意两个集合.那么可以十分精确的找到每一对相似的集合,但是时间复杂度是O(n2).当N比较小时,比如K级,此算法可以在接受的时间范 ...
- Spring MVC配置静态资源和资源包
Spring MVC配置静态资源和资源包 本例映射:css目录: pom.xml <properties> <spring.version>4.3.5.RELEASE</ ...
- 拖拉记录上下移动--Ajax UI
所谓的 Ajax 拖拉 UI,就是直接用鼠标进行拖拉排序,这种方式对用户来说操作速度更快. 拖拉的 UI 需要额外的前端套件,这里介绍 jQuery UI 的 Sortable Plugin,并直接使 ...
- baseCss/resetCss(转)
原文链接:https://github.com/hangyangws/baseCss#basecss baseCss 意义 统一各个浏览器差异.统一团队开发起始标准.弥补浏览器的“缺点”.提供频繁使用 ...
- 使用POI导入小数变成浮点数异常
例如 我在Excel中的数据为17.2, 导入到表中就变成了17.1999999或者17.20000001 原因是我用double接收了17.2,然后直接用了String去转换,精度就丢失了. 代 ...
- pos 和 AnsiPos
PropsClearList[I]的值是 用户=个人 R := AnsiPos(Equal_sign, PropsClearList[I]); ShowMessage( IntToStr( R));/ ...
- 进程间通信IPC
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 客户端链接Blog
Word 2013链接Blog 1.设置共享->发布至博客->发布至博客 2.新建博客账户 3.添加新建账户信息 注意:URL内为Blog名,而非用户名 a) 在"Type of ...