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.分 ...
随机推荐
- 20145221 《Java程序设计》第四周学习总结
20145221 <Java程序设计>第四周学习总结 教材学习内容总结 第六章部分 - 继承与多态 何谓继承 继承 继承是Java程序设计语言面向对象的又一重要体现,允许子类继承父类,避免 ...
- 高级bash脚本编程(三)
高级bash脚本编程 知识点 compound 和 comparison -a 逻辑与 exp1 -a exp2 如果表达式 exp1 和 exp2 都为真的话,那么结果为真. -o 逻辑或 exp1 ...
- Makefile解析(最简单的LED)
①led_sp.bin: start.o led.o #led_sp.bin是由 start.o 和 led.o 生成 ②arm-linux-ld -Ttext 0x0 -o led_sp.elf $ ...
- 【postman】利用谷歌浏览器插件生成代码
Postman这款工具可以让你很方便的测试你的Web API,那如果你实在是没办法用Postman,必须手写代码,又或者你有别的需求是Postman没法实现的,你必须写一个特殊的script或App来 ...
- Python学习札记(二十五) 函数式编程6 匿名函数
参考:匿名函数 NOTE 1.Python对匿名函数提供了有限的支持. eg. #!/usr/bin/env python3 def main(): lis = list(map(lambda x: ...
- tar: Cowardly refusing to create an empty archive 问题
在解压 .tar.gz文件的时候遇到了这个解压的问题. 原命令:tar -zcvf ···.tar.gz 提示:tar: Cowardly refusing to create an empty ar ...
- ros 杀掉所有节点
rosnode kill -a 或者 rosnode kill --all
- adb 安装软件
一.连接 adb connect 192.168.1.10 输出 connected to 二.查看设备 adb devices 输出 List of devices attached device ...
- Qt570_CentOS64x64_02
1.Qt570的简单测试项目,在做"重新构建"的操作的时候,出现1个问题,Qt底下的"编译输出"窗口中的信息为: cc1plus: error: unrecog ...
- Java Spring-JdbcTemplate
2017-11-10 22:55:45 Spring 对持久层技术支持 : JDBC : org.springframework.jdbc.core.JdbcTemplate Hibernate3.0 ...