String類的寫時(shí)拷貝實(shí)例
更新時(shí)間:2017年01月18日 10:33:21 投稿:jingxian
下面小編就為大家?guī)?lái)一篇String類的寫時(shí)拷貝實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
實(shí)例如下:
#include<iostream>
using namespace std;
class String;
ostream& operator<<(ostream &out, const String&s);
//引用計(jì)數(shù)器類
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所指的空間,在釋放之前調(diào)動(dòng)這個(gè)類的析構(gòu)函數(shù)
}
}
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; //這會(huì)更改引用計(jì)數(shù)器指針 ,造成s內(nèi)存泄漏
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; //引用計(jì)數(shù)器
};
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類的寫時(shí)拷貝實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
c字符串,string對(duì)象,字符串字面值的區(qū)別詳解
以下是對(duì)c字符串,string對(duì)象,字符串字面值的區(qū)別進(jìn)行了詳細(xì)的介紹,需要朋友可以 過(guò)來(lái)參考下2013-09-09
淺談C語(yǔ)言中的指針和數(shù)組有什么區(qū)別
C語(yǔ)言中的指針和數(shù)組是兩個(gè)重要的數(shù)據(jù)結(jié)構(gòu),它們?cè)趦?nèi)存管理和數(shù)據(jù)存儲(chǔ)方面有許多相似之處,但也存在一些關(guān)鍵的區(qū)別,本文就來(lái)介紹一下C語(yǔ)言中的指針和數(shù)組有什么區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
C語(yǔ)言使用realloc函數(shù)實(shí)現(xiàn)通訊錄源碼分析
什么是動(dòng)態(tài)通訊錄,就是在靜態(tài)的基礎(chǔ)上改進(jìn)了一下,不在使用數(shù)組,而是使用指針和動(dòng)態(tài)內(nèi)存開辟的函數(shù),當(dāng)空間不夠的時(shí)候,便進(jìn)行增容2023-02-02
C語(yǔ)言深入探索數(shù)據(jù)類型的存儲(chǔ)
使用編程語(yǔ)言進(jìn)行編程時(shí),需要用到各種變量來(lái)存儲(chǔ)各種信息。變量保留的是它所存儲(chǔ)的值的內(nèi)存位置。這意味著,當(dāng)您創(chuàng)建一個(gè)變量時(shí),就會(huì)在內(nèi)存中保留一些空間。您可能需要存儲(chǔ)各種數(shù)據(jù)類型的信息,操作系統(tǒng)會(huì)根據(jù)變量的數(shù)據(jù)類型,來(lái)分配內(nèi)存和決定在保留內(nèi)存中存儲(chǔ)什么2022-07-07
C語(yǔ)言編程技巧 關(guān)于const和#define的區(qū)別心得
盡量用const和inline而不用#define 這個(gè)條款最好稱為:“盡量用編譯器而不用預(yù)處理”,因?yàn)?define經(jīng)常被認(rèn)為好象不是語(yǔ)言本身的一部分。這是問題之一。再看下面的語(yǔ)句:2013-02-02
詳細(xì)解析C語(yǔ)言中的開方實(shí)現(xiàn)
這篇文章主要介紹了詳細(xì)解析C語(yǔ)言中的開方實(shí)現(xiàn),包括一道要求精度的整數(shù)開方的題目,需要的朋友可以參考下2015-08-08

