#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. Ubuntu16.04下配置pip国内镜像源加速安装【转】

    本文转载自:https://blog.csdn.net/yucicheung/article/details/79095742 问题描述 基于国内网速的问题,我们直接pip安装包通常速度非常慢,而且经 ...

  2. linux使用vi浏览python源码

    一.背景 2018/8/15,这一天要分析一个python项目,因此需要浏览代码,而我使用的是ubuntu 16.04,于是作此文 二.步骤 2.1 获取生成tags文件的脚本 http://svn. ...

  3. [JS] - level8 kata

    https://www.codewars.com/kata/57e3f79c9cb119374600046b function hello(name) { if(name == "" ...

  4. sonar runner的配置

    #Configure here general information about the environment, such as SonarQube DB details for example ...

  5. 发起图片请求的几种可能性(webkit内核)

    网页测试源代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...

  6. 与R纠缠的两件事——rownames和子集--转载

    与R语言纠缠了一个星期,从快速上手的暗暗得意,到之后某些细节的纠结烦躁,过山车式体验中,我逐渐才认识了真实的R语言. 期间遇到两个很烦恼的问题,一个是让人烦躁抓狂,另一个是无意发现的重大错误. 1.  ...

  7. hdu 4747 mex 线段树+思维

    http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意: 我们定义mex(l,r)表示一个序列a[l]....a[r]中没有出现过得最小的非负整数, 然后我 ...

  8. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp

    D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...

  9. codeforces 9 div2 C.Hexadecimal's Numbers 暴力打表

    C. Hexadecimal's Numbers time limit per test 1 second memory limit per test 64 megabytes input stand ...

  10. linux突然断电重启,配置文件丢失/程序无法打开/文件损坏

    电脑突然断电,重新开机后发现有的程序无法正常启动,原因是配置文件损坏了.感觉奇怪,为什么在硬盘里的文件会内容丢失? 1.可能:写数据的过程被中断,只完成了一部分.可能会出现乱码(因为只写了几个字节,不 ...