实例如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
#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;
}
|
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 72
-
2025-06-04 64
-
2025-05-25 25
-
2025-05-25 34
-
2025-05-29 91
热门评论

