String类的写时拷贝实例

 更新时间:2017年01月18日 10:33:21   投稿:jingxian  
下面小编就为大家带来一篇String类的写时拷贝实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

实例如下:

#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类的写时拷贝实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • C语言数据结构与算法时间空间复杂度基础实践

    C语言数据结构与算法时间空间复杂度基础实践

    这篇文章主要为大家介绍了C语言数据结构与算法中时间空间复杂度的基础实践,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-02-02
  • C++中volatile和mutable关键字用法详解

    C++中volatile和mutable关键字用法详解

    这篇文章主要介绍了C++中volatile和mutable关键字用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • C++中TinyXML读取xml文件用法详解

    C++中TinyXML读取xml文件用法详解

    本文主要介绍了C++中TinyXML读取xml文件用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • C语言sizeof和strlen区别小结

    C语言sizeof和strlen区别小结

    C语言中的sizeof和strlen是两个常用的操作符/函数,但它们的功能和用途有很大的区别,本文就详细的来介绍一下C语言sizeof和strlen区别,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • c++实现LinkBlockedQueue的问题

    c++实现LinkBlockedQueue的问题

    这篇文章主要介绍了c++实现LinkBlockedQueue的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • C++ 中友元函数与友元类详解

    C++ 中友元函数与友元类详解

    这篇文章主要介绍了C++ 中友元函数与友元类详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • 一篇文章带你了解c++运算符重载

    一篇文章带你了解c++运算符重载

    下面小编就为大家带来一篇深入理解C++运算符重载。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-08-08
  • C语言函数的参数使用指针

    C语言函数的参数使用指针

    这篇文章主要介绍了C语言函数的参数使用指针,本文讲述了指针在作为函数参数时候的使用方法,解析值传递和值引用的区别案例,希望对你有所帮助
    2021-06-06
  • 深入理解goto语句的替代实现方式分析

    深入理解goto语句的替代实现方式分析

    本篇文章是对goto语句的替代实现方式进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C语言格式输出二进制的2种方法总结

    C语言格式输出二进制的2种方法总结

    众所周知C中以八进制,十进制和十六进制都可以通过%o,%d和%x轻松实现,然而唯独没有提供二进制输出的快速方式,下面这篇文章主要给大家介绍了关于C语言格式输出二进制的2种方法,需要的朋友可以参考下
    2022-08-08

最新评论