#include <iostream>
#include <string.h> using namespace std; class String
{
char* m_data;
public:
String(const char* p = NULL)
{
if(p == NULL)
{
m_data = new char[];
*m_data = '\0';
}
else
{
m_data = new char[strlen(p) + ];
strcpy(m_data, p);
}
} String(const String & other)
{
if(&other != this)
{
//delete [] m_data; //¹¹Ô캯ÊýÖв»ÐèÒªdelete momory:w
m_data = new char[strlen(other.m_data) + ];
strcpy(m_data, other.m_data);
}
} ~String()
{
delete [] m_data;
} String& operator=(const String & other)
{
if(&other != this)
{
delete [] m_data;
m_data = new char[strlen(other.m_data) + ];
strcpy(m_data, other.m_data);
}
return *this; }
friend String operator+(const String &s1, const String &s2);
friend inline ostream & operator << (ostream & os, String &str); }; String operator+(const String &s1, const String &s2)
{
String temp;
delete [] temp.m_data; // temp.data Êǽöº¬¡®\0¡¯µÄ×Ö·û´®
temp.m_data = new char[strlen(s1.m_data) + strlen(s2.m_data) +];
strcpy(temp.m_data, s1.m_data);
strcat(temp.m_data, s2.m_data);
return temp;
} ostream & operator << (ostream & os, String &str)
{
os << str.m_data << endl;
return os;
} int main()
{
String str1("abc");
cout << str1; String str2(str1);
cout << str2; String str3;
cout << str2;
str3 = str2; String str4("def"); String str5;
str5 = str3 + str4;
cout << str5;
}

从String类看写C++ class需要注意的地方的更多相关文章

  1. String类的写时拷贝

    #include<iostream>using namespace std; class String;ostream& operator<<(ostream & ...

  2. (转)C++——std::string类的引用计数

    1.概念 Scott Meyers在<More Effective C++>中举了个例子,不知你是否还记得?在你还在上学的时候,你的父母要你不要看电视,而去复习功课,于是你把自己关在房间里 ...

  3. 使用引用计数和copy-on_write实现String类

    本文写于2017-01-18,从老账号迁移到本账号,原文地址:https://www.cnblogs.com/huangweiyang/p/6295420.html 这算是我开始复习的内容吧,关于st ...

  4. 扩展C++ string类

    在实际开发过程中,C++string类使用起来有很多不方便的地方,笔者根据根据这些不足简单的扩展了这个类,如增加与数字之间的相互转化和格式化字符串.不足的地方望指正.读者也可以根据自己需求继续扩展. ...

  5. cpp 实现简易String类

    需求 实现一个String类 自己写的String headers/String.h #ifndef __MYSTRING__ #define __MYSTRING__ #include <st ...

  6. String类源码分析

    1.String类注释说明 /** * The {@code String} class represents character strings. All * string literals in ...

  7. String类的实现(4)写时拷贝浅析

    由于释放内存空间,开辟内存空间时花费时间,因此,在我们在不需要写,只是读的时候就可以不用新开辟内存空间,就用浅拷贝的方式创建对象,当我们需要写的时候才去新开辟内存空间.这种方法就是写时拷贝.这也是一种 ...

  8. String 类的实现(2)引用计数与写时拷贝

    1.引用计数 我们知道在C++中动态开辟空间时是用字符new和delete的.其中使用new test[N]方式开辟空间时实际上是开辟了(N*sizeof(test)+4)字节的空间.如图示其中保存N ...

  9. 自己实现简单的string类

    1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...

随机推荐

  1. [Android开发系列]IT博客应用V1.3

    首先,感谢使用这款软件并给我意见的朋友们,有你们的意见,才有了这个版本. 其次,检索功能和分类筛选功能(如果是你提的意见,记得在下面mark哦,毕竟读代码你能发现,其实发意见这个就是用自己的邮箱给自己 ...

  2. 20150414---ListView简介(web)

    ListView,自带分页功能,而且用户自定义界面样式自由度高. 如下图,都是使用Listview完成的,(测试数据,内容较乱) 所在位置:工具--数据-ListView 这里是配置ListView样 ...

  3. 杭电ACM2096--小明A+B

    http://acm.hdu.edu.cn/showproblem.php?pid=2096 本来就是很简单.但是数据的大小有要求. (a%100+b%100)%100和(a+b)%100本来没有什么 ...

  4. Spark RDD整理

    参考资料: Spark和RDD模型研究:http://itindex.net/detail/51871-spark-rdd-模型 理解Spark的核心RDD:http://www.infoq.com/ ...

  5. 【风马一族_Python】 更替pip的版本

    替换电脑上python中的pip的版本 例子: 下载的文件:pip-8.1.1-py2.py3-none-any.whl 下载地址:https://pypi.python.org/pypi/pip/# ...

  6. HTML5-Geolocation&地图.html

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. Shell脚本升级CentOS php版本v

    #! /bin/sh #1.关闭selinuxcp -rp /etc/selinux/config /etc/selinux/config.baksetenforce 0sed -i '7s/enfo ...

  8. 如何判断PHP 是ts还是nts版的

    通过phpinfo(); 查看其中的 Thread Safety 项,这个项目就是查看是否是线程安全,如果是:enabled,一般来说应该是ts版,否则是nts版.

  9. Linux 下常用的压缩,解压方法

    压缩命令: tar.gz 格式: tar -zcvf  自定义压缩文件名.tar.gz   被压缩文件名 zip 格式: zip -r 自定义压缩文件名.zip 被压缩文件名 如果要压缩整个文件夹,也 ...

  10. 解决Win7下运行php Composer出现SSL报错的问题

    以前都在linux环境使用php composer.今天尝试在win7下运行composer却出现SSL报错: D:\data\www\mmoyu\symapp>php -f %phprc%\c ...