程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> String類的寫時拷貝實例

String類的寫時拷貝實例

編輯:關於C++

String類的寫時拷貝實例。本站提示廣大學習愛好者:(String類的寫時拷貝實例)文章只能為提供參考,不一定能成為您想要的結果。以下是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類的寫時拷貝實例就是分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved