欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

String类的写时拷贝实例

时间:2021-01-06 11:24:19|栏目:C代码|点击:

实例如下:

#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;
}

上一篇:纯C语言:检索与周游广度深度遍历源码分享

栏    目:C代码

下一篇:C++中vector容器使用详细说明

本文标题:String类的写时拷贝实例

本文地址:http://www.codeinn.net/misctech/41548.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有