String類的寫時(shí)拷貝實(shí)例
實(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++函數(shù)對(duì)象Functor與匿名函數(shù)對(duì)象Lambda表達(dá)式詳解
這篇文章主要介紹了C++函數(shù)對(duì)象Functor(仿函數(shù))與匿名函數(shù)對(duì)象(Lambda表達(dá)式)詳細(xì)介紹以及底層實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
C/C++如何實(shí)現(xiàn)循環(huán)左移,循環(huán)右移
這篇文章主要介紹了C/C++如何實(shí)現(xiàn)循環(huán)左移,循環(huán)右移,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
OpenCV透視變換應(yīng)用之書本視圖矯正+廣告屏幕切換
透視變換是指利用透視中心、像點(diǎn)、目標(biāo)點(diǎn)三點(diǎn)共線的條件,按透視旋轉(zhuǎn)定律使承影面繞跡線旋轉(zhuǎn)某一角度,破壞原有的投影光線束,仍能保持承影面上投影幾何圖形不變的變換。本文將為大家介紹兩個(gè)OpenCV透視變換應(yīng)用,需要的可以參考一下2022-08-08
約瑟夫經(jīng)典問(wèn)題擴(kuò)展成雙向約瑟夫問(wèn)題
今天小編就為大家分享一篇關(guān)于約瑟夫經(jīng)典問(wèn)題擴(kuò)展成雙向約瑟夫問(wèn)題,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
深入解析C語(yǔ)言中的內(nèi)存分配相關(guān)問(wèn)題
這篇文章主要深入地介紹了C語(yǔ)言中的內(nèi)存分配,C語(yǔ)言編程中的內(nèi)存泄漏問(wèn)題一直以來(lái)都是C編程中的一大棘手問(wèn)題,本文從malloc和指針等方面對(duì)C內(nèi)存進(jìn)行了深層次講解,強(qiáng)烈推薦!需要的朋友可以參考下2015-08-08
由static_cast和dynamic_cast到C++對(duì)象占用內(nèi)存的全面分析
下面小編就為大家?guī)?lái)一篇由static_cast和dynamic_cast到C++對(duì)象占用內(nèi)存的全面分析。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
Matlab實(shí)現(xiàn)繪制立體玫瑰花的示例代碼
這篇文章主要介紹了如何利用Matlab實(shí)現(xiàn)繪制更立體的玫瑰花,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Matlab有一定的幫助,需要的可以參考一下2023-02-02

