#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. 第十一篇、微信小程序-input组件

    主要属性: 效果图: ml: <!--style的优先级比class高会覆盖和class相同属性--> <!--头像--> <view style="displ ...

  2. Xcode - 方法注释插件

    VVDocumenter-Xcode,自动生成注释,感觉比较方便的插件,分享下,应该很多人都知道= = 在 https://github.com/onevcat/VVDocumenter-Xcode  ...

  3. 【Unity3D】刚体与碰撞体以及is Trigger属性的意义

    [Unity3D]刚体与碰撞体以及is Trigger属性的意义 刚体:个人理解就是具有物理属性(如:质量),接受物理作用(如:重力)的组件. 碰撞体:个人理解就是计算碰撞后的物理量(如:弹力). 刚 ...

  4. id,class,name区别

    id,class,name区别 id:标签唯一标识,好比我们身份证号码,具有唯一性.JS常用document,getGlementBy(id). class:标签的类别,可重复使用,CSS常用. na ...

  5. 解决win7 下 curl无法加载的问题

    最近分别在WIN7和Windows8 上分别安装php 高版本!都遇到了这个问题! 一.win7系统64位, apache2.2, php 5.35 vc6 版本 这个比较容易: 1. phpinfo ...

  6. [zz] Install VSFTP

    The first two letters of vsftpd stand for "very secure" and the program was built to have ...

  7. c++对象内存布局

    这篇文章我要简单地讲解下c++对象的内存布局,虽然已经有很多很好的文章,不过通过实现发现有些地方不同的编译器还是会有差别的,希望和大家交流. 在没有用到虚函数的时候,C++的对象内存布局和c语言的st ...

  8. IE8浏览器跨域接口访问异常的解决办法

    IE8版本以下浏览器绝对是一个神奇的存在,忙碌好久,万事具备,居然在ajax调用接口的时候直接爆炸 陈述一下问题 首先是有这样一个接口,请求类型POST,入参JSON,出参JSON,jQuery aj ...

  9. 利用Apply的参数数组化来提高代码的优雅性,及高效性

    利用Apply的参数数组化来提高代码的优雅性,及高效性 Function.apply()在提升程序性能方面的技巧 我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所 ...

  10. 为msysgit增加vim语法高亮文件

    在win7下装了msysgit,今天我遇到一个不爽的问题,打开git bash,用vim打开一个xml文件 结果都是黑屏的,没语法高亮,这个必须不能忍啊,我找到msysgit的安装目录,发现Vim73 ...