引用计数——深拷贝&浅拷贝

引用计数——深拷贝&浅拷贝

下面是用代码实现:

private:
char *data;
size_t use_count;
public:
//构造函数
String_rep(const char *str = ""):use_count()
{
if(str == NULL)
{
data = new char[];
data[] = '\0';
}
else
{
data = new char[strlen(str)+];
strcpy(data,str);
}
}
//拷贝构造函数
String_rep(const String_rep &rep);
//析构函数
~String_rep()
{
if(use_count == )
{
delete[] data;
data = NULL;
}
}
private:
String_rep *rep;
public:
String(const char *str = ""):rep(new String_rep(str))
{
rep->increment();
}
String(const String &s)
{
rep = s.rep;
rep->increment();
}
~String()
{
rep->decrement();
}

注意理解下面这一段:(调试记录图)

  //析构函数
~String_rep()
{
if(use_count == )
{
delete[] data;
data = NULL;
}
}

引用计数——深拷贝&浅拷贝

还要注意一点,如果要修改其中的内容的话就得先开一块空间,把值拷贝过来然后,再在里面进行修改。

eg:

void upper()
{
rep->decrement();
rep = new String_rep(rep->data); int n = strlen(rep->data);
for(int i=;i<n;++i)
{
if(rep->data[i] >='a'&&rep->data[i]<='z')
rep->data[i] = rep->data[i] - ;
}
rep->increment(); }

以String类的实现来做个示例:

#pragma once
#include<iostream>
using namespace std; class String;
ostream& operator<<(ostream &out, const String &s);
void Turn(String &s); class String_rep
{
friend void Turn(String &s);
friend class String;
friend ostream& operator<<(ostream &out, const String &s);
private:
char *data;
size_t use_count;
public:
//构造函数
String_rep(const char *str = ""):use_count()
{
if(str == NULL)
{
data = new char[];
data[] = '\0';
}
else
{
data = new char[strlen(str)+];
strcpy(data,str);
}
}
//拷贝构造函数
String_rep(const String_rep &rep);
//析构函数
~String_rep()
{
if(use_count == )
{
delete[] data;
data = NULL;
}
}
public:
void increment()
{
++use_count;
}
void decrement()
{
--use_count;
}
};
class String
{
friend void Turn(String &s);
friend ostream& operator<<(ostream &out, const String &s);
private:
String_rep *rep;
public:
String(const char *str = ""):rep(new String_rep(str))
{
rep->increment();
}
String(const String &s)
{
rep = s.rep;
rep->increment();
}
~String()
{
rep->decrement();
}
String &operator = (const String &s)
{
if(this !=&s)
{
rep->decrement();
rep = s.rep;
rep->increment();
}
return *this;
}
String &operator+(const String &s1)
{
char *tempch;
int n = strlen(rep->data)+strlen(s1.rep->data)+;
tempch = new char[n];
strcpy(tempch,rep->data);
strcat(tempch,s1.rep->data);
String *temp;
temp = new String(tempch);
temp->rep->decrement();
return *temp;
}
String &operator+=(const String &s1)
{
char *tempch;
int n = strlen(rep->data)+strlen(s1.rep->data)+;
tempch = new char[n];
strcpy(tempch,rep->data);
strcat(tempch,s1.rep->data); String_rep *temp;
temp = new String_rep(tempch);
int remsize = rep->use_count;
rep = temp;
rep->use_count = remsize;
delete tempch;
return *this;
}
void upper()
{
rep->decrement();
rep = new String_rep(rep->data); int n = strlen(rep->data);
for(int i=;i<n;++i)
{
if(rep->data[i] >='a'&&rep->data[i]<='z')
rep->data[i] = rep->data[i] - ;
}
rep->increment(); }
};
ostream& operator<<(ostream &out, const String &s)
{
out<<s.rep->data;
return out;
}

使用*:

#include"use_count_string.h"
int main()
{
String st1("asdfghj");
String st2(st1);
String st6(st1);
String st7(st1);
String st3("qwerty");
String st5("Anna");
String st4;
st4 = st1 + st3;
st5 += st3;
st1 = st3;
cout<<"st1 = "<<st1<<endl;
cout<<"st2 = "<<st2<<endl;
cout<<"st3 = "<<st3<<endl;
cout<<"st4 = "<<st4<<endl;
cout<<"st5 = "<<st5<<endl;
st2.upper();
cout<<"st2 = "<<st2<<endl;
return ;
}
上一篇:Genymotion无法启动Virtual Box


下一篇:mybatis整合hikariCP(非spring)