#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类的简要实现的更多相关文章

  1. String类的简要概述(1)

    String类时我们平时用的比较多的一个类,该类属于java中引用数据类型. String类里面有很多方法需要我们学习.如切割,追加,拼接等. String s = "abcdef" ...

  2. JAVA集合类简要笔记 - 内部类 包装类 Object类 String类 BigDecimal类 system类

    常用类 内部类 成员内部类.静态内部类.局部内部类.匿名内部类 概念:在一个类的内部再定义一个完整的类 特点: 编译之后可生成独立的字节码文件 内部类可直接访问外部类私有成员,而不破坏封装 可为外部类 ...

  3. 标准C++中的string类的用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

  4. VC++ 标准C++中的string类的用法总结

    相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...

  5. [C++][语言语法]标准C++中的string类的用法总结

    转自:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 要想使用标准C++中string类,必须要包含 #include ...

  6. 标准C++中string类的用法

    转自博客园:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非 ...

  7. 标准C++中的string类的用法总结(转)

    http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的 ...

  8. C++中的string类(2)

    相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...

  9. 浅析String类

    这是对于String类的一些总结,我将会从几个方面并且结合着字符串池等相关知识进行总结 进程如下:                1.对于String类基本知识的一些总结 2.简要介绍字符串池 3.分 ...

随机推荐

  1. 微信小程序:页面配置 page.json

    微信小程序:页面配置 page.json 一.页面配置 page.json 如果整个小程序的风格是蓝色调,那么可以在 app.json 里边声明顶部颜色是蓝色即可. 实际情况可能不是这样,可能你小程序 ...

  2. Python3.x的BeautifulSoup解析html常用函数

    Python3.x的BeautifulSoup解析html常用函数 1,初始化: soup = BeautifulSoup(html) # html为html源代码字符串,type(html) == ...

  3. Stitching模块中focalsFromHomography初步研究

    在Stitching模块中,通过“光束法平差”的时候,有一个步骤为“通过单应矩阵估算摄像头焦距”,调用的地方为:   , ));    ] ];    d2 ] ]) ] ]);    v1 ] ]  ...

  4. 20145315 《Java程序设计》第五周学习总结

    20145315 <Java程序设计>第五周学习总结 教材学习内容总结 第八章 8.1语法与继承架构 8.1.1使用try,catch 所有的错误都会被打包为对象,使用try,catch可 ...

  5. RC 522模块在LINUX平台调试笔记

    硬件平台: 1 主控:SMDK Exynos4412 POP S5M8767A 2 RFID模块:君盾集团提供的RC522模块 3 通信接口:SPI 软件平台:Android ICS & ke ...

  6. Today's harvest !!!

    今天将Mybatis的视频看到了第60集,其之前讲解了自表的主外键查询.例如一个新闻表中,有一级栏目,二级栏目,三级栏目,其中二级栏目的pid为一级栏目的id,如此种种. 而今天做的小项目中使用了 e ...

  7. POJ 1034 The dog task(二分图匹配)

    http://poj.org/problem?id=1034 题意: 猎人和狗一起出去,狗的速度是猎人的两倍,给出猎人的路径坐标,除了这些坐标外,地图上还有一些有趣的点,而我们的狗,就是要尽量去多的有 ...

  8. python爬虫scrapy学习之篇二

    继上篇<python之urllib2简单解析HTML页面>之后学习使用Python比较有名的爬虫scrapy.网上搜到两篇相应的文档,一篇是较早版本的中文文档Scrapy 0.24 文档, ...

  9. [翻译]PyMongo官方文档

    PyMongo官方文档翻译 周煦辰 2016-06-30 这是本人翻译的PyMongo官方文档.现在网上分(抄)享(袭)的PyMongo博客文章很多,一方面这些文章本就是抄袭的,谈不上什么格式美观,另 ...

  10. oracle: sql语句报ora-01461/ora-00911错误

    oracle: sql语句报ora-01461/ora-00911错误 ora-00911:sql语句中可能含有特殊字符,或者sql语句中不能用";"分号结尾. sql语句报ora ...