#include<iostream>
using namespace std;
 
class String;
ostream& operator<<(ostream &out, const String&s);
//引用计数器类
class String_rep 
{
    friend class String;
    friend ostream& operator<<(ostream &out, const String&s);
public:
        String_rep(const char *str )
            :use_count(0)
        {
            if (str == NULL)
            {
                data = new char[1];
                data[0] = '\0';
            }
            else
            {
                data = new char[strlen(str) + 1];
                strcpy(data, str);
            }
        }
    
        String_rep(const String_rep  &rep) :use_count(0)
        {
            data = new  char[strlen(rep.data) + 1];
            strcpy(data, rep.data);
        }
        String_rep&  operator=(const String_rep &rep)
        {
            if (this != &rep)
            {
                delete[]data;
                data = new char[strlen(rep.data) + 1];
                strcpy(data, rep.data);
            }
            return *this;
        }
        ~String_rep()
        {
                delete[]data;
                data = NULL;
        }
public:
    void increase()
    {
        ++use_count;
    }
    
    void decrease()
    {
        if (use_count == 0)
        {
            delete this; //自杀行为    释放this所指的空间,在释放之前调动这个类的析构函数
        }
    }
private:
        char *data;
        int use_count;
};
////////////////////////////////////////////////////////////////////////////////////////
class String
{
     friend ostream& operator<<(ostream &out, const String&s);
public:
    String(const char* str = " ")
    {
        rep = new String_rep(str);
        rep->increase();
    }
    String(const String &s)
    {
        rep = s.rep;      //浅拷贝
        rep->increase();
    }
    String& operator=(const String &s)
    {
        if (this != &s)
        {
            rep->decrease();   //模拟delete
            rep = s.rep;           //模拟new
            rep->increase();      //模拟strcpy
            /*rep = s.rep;   //这会更改引用计数器指针 ,造成s内存泄漏
            rep->increase();*/
        }
        return *this;
    }
        ~String()
        {
            rep->decrease();
        }
public:
    void to_upper()
    {
        if (rep->use_count > 1)
        {
            String_rep*  new_rep = new String_rep(rep->data);
            rep->decrease();
            rep = new_rep;
            rep->increase();
        }
        char* ch = rep->data;
        while (*ch != '\0')
        {
            *ch -= 32;
            ++ch;
        }
    }
private:
    String_rep *rep;  //引用计数器
};
ostream& operator<<(ostream &out, const String&s)
{
    out << s.rep->data;
    return out;
}
void main()
{
    String s1("hello");
    String s2(s1);
    String s3;
    s3 = s2;
    cout << "s1=" << s1 << endl;
    cout << "s2=" << s2 << endl;
    cout << "s3=" << s3 << endl;

s2.to_upper();

cout << "-----------------------------------------------" << endl;
   
    cout << "s1=" << s1 << endl;
    cout << "s2=" << s2 << endl;
    cout << "s3=" << s3 << endl;
}

String类的写时拷贝的更多相关文章

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

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

  2. 标准C++类std::string的内存共享和Copy-On-Write(写时拷贝)

    标准C++类std::string的内存共享,值得体会: 详见大牛:https://www.douban.com/group/topic/19621165/ 顾名思义,内存共享,就是两个乃至更多的对象 ...

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

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

  4. String写时拷贝实现

    头文件部分 1 /* 版权信息:狼 文件名称:String.h 文件标识: 摘 要:对于上版本简易的String进行优化跟进. 改进 1.(将小块内存问题与大块分别对待)小内存块每个对象都有,当内存需 ...

  5. 计算机程序的思维逻辑 (73) - 并发容器 - 写时拷贝的List和Set

    本节以及接下来的几节,我们探讨Java并发包中的容器类.本节先介绍两个简单的类CopyOnWriteArrayList和CopyOnWriteArraySet,讨论它们的用法和实现原理.它们的用法比较 ...

  6. 并发容器之写时拷贝的 List 和 Set

    对于一个对象来说,我们为了保证它的并发性,通常会选择使用声明式加锁方式交由我们的 Java 虚拟机来完成自动的加锁和释放锁的操作,例如我们的 synchronized.也会选择使用显式锁机制来主动的控 ...

  7. Java编程的逻辑 (73) - 并发容器 - 写时拷贝的List和Set

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  8. 写时拷贝(Copy On Write)方案详解

    本文旨在通过对 写时拷贝 的四个方案(Copy On Write)分析,让大家明白写时拷贝的实现及原理. 关于浅拷贝与深拷贝,我在之前的博客中已经阐述过了  浅拷贝容易出现指针悬挂的问题,深拷贝效率低 ...

  9. rust漫游 - 写时拷贝 Cow<'_, B>

    rust漫游 - 写时拷贝 Cow<'_, B> Cow 是一个写时复制功能的智能指针,在数据需要修改或者所有权发生变化时使用,多用于读多写少的场景. pub enum Cow<'a ...

随机推荐

  1. 原生js如何获取当前所加载网页的文件路径和名称

    结合使用string对象中的substr()和lastIndexOf()方法. 当前页面路径:file:///C:/Users/Administrator/Desktop/test.html < ...

  2. aop前传之代理

    一.jdk提供proxy类对目标对象实现代理,简单的说对方法的调用交给代理对象来操作. 代理目标 代理的具体实现: 代理测试; 简单说:利用proxy生成一个委托类实现代理.这个委托类是目标类的接口的 ...

  3. Web App时代的缓存机制新思路

    Web App常见架构 以WebQQ例,WebQQ这个站点的所有内容都是一个页面里面呈现的,我们看到的类似windows操作系统的框架,是它的顶级容器和框架,由AlloyOS的内核负责统筹和管理,然后 ...

  4. Ubuntu 12.04下PostgreSQL-9.1安装与配置详解(在线安装)

    说明:       我是用root用户在终端登陆的,如果是非root用户,那在命令前需要加上"sudo",你懂的... 第一步:在Ubuntu下安装Postgresql       ...

  5. hdu 4414 Finding crosses

    题目链接:hdu 4414 其实是一道简单的字符型水题,不涉及任何算法,可比赛时却没能做出来,这几天的状态都差到家了... 题目大意是求有多少个满足条件的十字架,十字架的边不能有分叉路口,所以枚举每个 ...

  6. ios8中,相册创建后手动删除,不能再进行创建显示

    // Add a new ALAssetsGroup to the library. // The name of the ALAssetsGroup is name and the type is ...

  7. Maven——Maven核心概念

    原文:http://www.cnblogs.com/xdp-gacl/p/4051819.html 一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1. ...

  8. LF will be replaced by CRLF in git add

    git add 出现这样的提示: LF will be replaced by CRLF in qinqiu.txt. 这个时候要: $ rm -rf .git  // 删除.git $ git co ...

  9. java 基础(第一天)

    1.  一个文件里面只能有一个 public 修饰的方法   且方法名与文件名保持一致. 如: public class main(){ } class car(){ } class dog(){ } ...

  10. SAP中寄售处理

    寄售分两种: 1, 供应商提供货物,我们销售 2,我们提供货物,寄售商销售 [@more@] 1, 供应商提供货物,我们销售 创建PO,购买寄售货物,categories维护成K,然后收货即可. 2, ...