DraculaW
C++博客
::
首页
::
新随笔
::
联系
::
聚合
::
管理
::
19 随笔 :: 0 文章 :: 7 评论 :: 0 Trackbacks
<
2024年11月
>
日
一
二
三
四
五
六
27
28
29
30
31
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
1
2
3
4
5
6
7
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(1)
给我留言
查看公开留言
查看私人留言
随笔档案
2008年11月 (1)
2007年11月 (18)
搜索
最新评论
1. re: 給出一個數組a[n] 求 第k大的值[未登录]
递归里面的findK(a, i+1, end);少参数
--hzx
2. re: Best Fit与 First Fit
应该只是一个效率的考虑吧。如果从碎片上讲的话,malloc之后总有mfree的,这样来看,best fit肯定比first fit好,如果逻辑上忘了mfree的话,会误解的。
--IAC002
3. re: 从csdn搬家过来了
如果和firefox不兼容,我将放弃csdn
--newsoftkit
4. re: 从csdn搬家过来了
好像是CSDN的程序有问题,内部不知道怎么处理的,一般慢一点也不至于浏览器死掉状态
--<a href=http://minidx.com>minidxer</a>
5. re: 从csdn搬家过来了
同感 因为打开csdn太慢了... 不知道他们怎么搞的
--DraculaW
阅读排行榜
1. Best Fit与 First Fit(1913)
2. deque 双向队列(1130)
3. STL与boost的type traits(922)
4. 給出一個數組a[n] 求 第k大的值(843)
5. 还是大学的作业(470)
评论排行榜
1. 从csdn搬家过来了(5)
2. Best Fit与 First Fit(1)
3. 給出一個數組a[n] 求 第k大的值(1)
4. 給一個數組 a[n] = {0, 1, 2, 3, 4, 5, 6} 一個k=3 // 將 a[n]變為 a[n] = {3, 4, 5, 6, 0, 1, 2}(0)
5. 链表倒置(0)
尝试写一个智能指针把
1
#include
"
SharedPtr.h
"
2
#include
"
gtest/gtest.h
"
3
4
using
namespace
std;
5
6
TEST(SharedPtr_test, TestConstructer)
7
{
8
SharedPtr
<
int
>
s1;
9
EXPECT_EQ(NULL, s1.
get
());
10
EXPECT_EQ(
0
, s1.use_count());
11
12
int
*
pi
=
new
int
;
13
SharedPtr
<
int
>
s2(pi);
14
EXPECT_EQ(pi, s2.
get
());
15
EXPECT_EQ(
1
, s2.use_count());
16
}
;
17
18
TEST(SharedPtr_test, TestAssign)
19
{
20
SharedPtr
<
int
>
s1;
21
22
int
*
pi
=
new
int
;
23
SharedPtr
<
int
>
s2(pi);
24
25
s1
=
s2;
26
EXPECT_EQ(s1.
get
(), s2.
get
());
27
EXPECT_EQ(s1.use_count(), s2.use_count());
28
29
EXPECT_EQ(pi, s1.
get
());
30
EXPECT_EQ(
2
, s1.use_count());
31
32
int
*
p3
=
new
int
;
33
SharedPtr
<
int
>
s3(p3);
34
s1
=
s3;
35
EXPECT_EQ(s1.
get
(), s3.
get
());
36
EXPECT_EQ(s1.use_count(), s3.use_count());
37
38
EXPECT_EQ(pi, s2.
get
());
39
EXPECT_EQ(
1
, s2.use_count());
40
41
s2
=
s3;
42
EXPECT_EQ(s1.
get
(), s2.
get
());
43
EXPECT_EQ(
3
, s2.use_count());
44
EXPECT_EQ(s1.use_count(), s2.use_count());
45
46
47
SharedPtr
<
int
>
s4;
48
SharedPtr
<
int
>
s5;
49
s4
=
s5;
50
EXPECT_EQ(s4.
get
(), s5.
get
());
51
EXPECT_EQ(s4.use_count(), s5.use_count());
52
53
SharedPtr
<
int
>
s6;
54
SharedPtr
<
int
>
s7(s6);
55
EXPECT_EQ(s6.
get
(), s7.
get
());
56
EXPECT_EQ(s6.use_count(), s7.use_count());
57
58
SharedPtr
<
int
>
s8;
59
SharedPtr
<
int
>
s9;
60
s8
=
s9;
61
int
*
p10
=
new
int
;
62
*
p10
=
10
;
63
SharedPtr
<
int
>
s10(p10);
64
s8
=
s10;
65
EXPECT_NE(s8.
get
(), s9.
get
());
66
EXPECT_NE(s8.use_count(), s9.use_count());
67
(
*
s10)
--
;
68
EXPECT_EQ(
9
,
*
(s8.
get
()));
69
70
}
;
71
72
//
TEST(SharedPtr_test, TestNULL)
73
//
{
74
//
SharedPtr<int> s1;
75
//
76
//
try{
77
//
s1.operator *();
78
//
}
79
//
catch(exception e)
80
//
{
81
//
EXPECT_EQ(e.what(), "There is a null pointer");
82
//
}
83
//
84
//
85
//
try{
86
//
s1.operator ->();
87
//
}
88
//
catch(exception e)
89
//
{
90
//
EXPECT_EQ(e.what(), "There is a null pointer");
91
//
}
92
//
}
93
94
TEST(SharedPtr_test, TestOperatorEqual)
95
{
96
SharedPtr
<
int
>
s1;
97
SharedPtr
<
int
>
s2;
98
SharedPtr
<
double
>
s3;
99
SharedPtr
<
string
>
s4;
100
101
int
*
p1
=
new
int
;
102
SharedPtr
<
int
>
s5(p1);
103
SharedPtr
<
int
>
s6(s5);
104
SharedPtr
<
int
>
s7
=
s6;
105
SharedPtr
<
int
>
s8;
106
s8
=
s7;
107
int
*
p2
=
new
int
;
108
SharedPtr
<
int
>
s9(p2);
109
110
111
EXPECT_EQ(
true
, s1
==
s2);
112
EXPECT_EQ(
false
, s1
==
s3);
113
EXPECT_EQ(
false
, s1
==
s4);
114
115
116
EXPECT_EQ(
true
, s5
==
s6);
117
EXPECT_EQ(
false
, s5
==
s2);
118
EXPECT_EQ(
true
, s5
==
s7);
119
EXPECT_EQ(
true
, s5
==
s8);
120
EXPECT_EQ(
false
, s5
==
s9);
121
}
122
123
TEST(SharedPtr_test, TestOperatorNotEqual)
124
{
125
SharedPtr
<
int
>
s1;
126
SharedPtr
<
int
>
s2;
127
SharedPtr
<
double
>
s3;
128
SharedPtr
<
string
>
s4;
129
130
int
*
p1
=
new
int
;
131
SharedPtr
<
int
>
s5(p1);
132
SharedPtr
<
int
>
s6(s5);
133
SharedPtr
<
int
>
s7
=
s6;
134
SharedPtr
<
int
>
s8;
135
s8
=
s7;
136
int
*
p2
=
new
int
;
137
SharedPtr
<
int
>
s9(p2);
138
139
140
EXPECT_NE(
true
, s1
!=
s2);
141
EXPECT_NE(
false
, s1
!=
s3);
142
EXPECT_NE(
false
, s1
!=
s4);
143
144
EXPECT_NE(
true
, s5
!=
s6);
145
EXPECT_NE(
false
, s5
!=
s2);
146
EXPECT_NE(
true
, s5
!=
s7);
147
EXPECT_NE(
true
, s5
!=
s8);
148
EXPECT_NE(
false
, s5
!=
s9);
149
}
150
151
TEST(SharedPtr_test, TestOther)
152
{
153
int
*
pi
=
new
int
;
154
SharedPtr
<
int
>
s1(pi);
155
EXPECT_EQ(pi, s1.
get
());
156
EXPECT_EQ(
1
, s1.use_count());
157
158
*
s1
=
10
;
159
EXPECT_EQ(
10
, (
*
(s1.
get
())));
160
EXPECT_EQ(
10
, (
*
s1));
161
162
UserCount
*
puc
=
new
UserCount(
19
);
163
SharedPtr
<
UserCount
>
sc(puc);
164
EXPECT_EQ(puc,
&
(
*
sc));
165
EXPECT_EQ(puc, sc.
get
());
166
EXPECT_EQ(
19
, sc
->
GetCount());
167
}
;
168
169
int
main(
int
argc,
char
**
argv)
170
{
171
testing::InitGoogleTest(
&
argc, argv);
172
173
return
RUN_ALL_TESTS();
174
return
0
;
175
}
上面是测试
下面是代码
1
#ifndef __SSA_SHARED_PTR__
2
#define
__SSA_SHARED_PTR__
3
4
#include
<
stdexcept
>
5
6
using
namespace
std;
7
8
class
NullPointerException: exception
{
9
public
:
10
NullPointerException():exception(
"
There is a null pointer
"
)
{}
;
11
}
;
12
13
template
<
typename T
>
14
class
UserObject
{
15
T
*
pObj;
16
17
public
:
18
UserObject(T
*
v):pObj(v)
{}
;
19
UserObject(
const
UserObject
<
T
>&
rl)
{
20
this
->
pObj
=
rl.pObj;
21
}
;
22
23
UserObject
&
operator
=
(
const
UserObject
<
T
>&
rl)
{
24
this
->
pObj
=
rl.pObj;
25
}
;
26
27
~
UserObject()
{delete pObj;}
;
28
29
T
&
operator
*
()
const
{
30
if
(pObj
==
NULL)
{
31
throw
NullPointerException();
32
}
33
return
*
pObj;
34
}
35
36
T
*
operator
->
()
const
{
37
if
(pObj
==
NULL)
{
38
throw
NullPointerException();
39
}
40
return
pObj;
41
}
42
43
T
*
get
()
const
{
44
return
pObj;
45
}
46
}
;
47
48
class
UserCount
{
49
public
:
50
51
UserCount():_userCount(
1
)
{}
;
52
UserCount(size_t count):_userCount(count)
{}
;
53
size_t AddRef()
{
54
_userCount
++
;
55
return
_userCount;
56
}
57
58
size_t Release()
{
59
_userCount
--
;
60
return
_userCount;
61
}
62
63
size_t GetCount()
{
64
return
_userCount;
65
}
66
67
private
:
68
size_t _userCount;
69
}
;
70
71
template
<
typename T
>
72
class
SharedPtr
{
73
public
:
74
SharedPtr()
{
75
pObj
=
new
UserObject
<
T
>
(NULL);
76
pn
=
new
UserCount(
0
);
77
}
;
78
79
SharedPtr(T
*
v)
{
80
pObj
=
new
UserObject
<
T
>
(v);
81
pn
=
new
UserCount(
1
);
82
}
;
83
84
SharedPtr(
const
SharedPtr
<
T
>
&
rl)
{
85
this
->
pObj
=
rl.pObj;
86
rl.pn
->
AddRef();
87
this
->
pn
=
rl.pn;
88
}
89
90
~
SharedPtr()
{
91
if
(
this
->
pn
==
NULL)
{
92
return
;
93
}
94
95
if
(
this
->
pn
->
Release()
==
0
)
{
96
delete
this
->
pObj;
97
delete
this
->
pn;
98
}
99
}
;
100
101
T
&
operator
*
()
const
{
102
return
pObj
->
operator
*
();
103
}
104
105
T
*
operator
->
()
const
{
106
return
pObj
->
operator
->
();
107
}
108
109
T
*
get
()
const
{
110
return
pObj
->
get
();
111
}
112
113
std::size_t use_count()
const
{
114
if
(pn
==
NULL)
return
0
;
115
return
pn
->
GetCount();
116
}
117
118
SharedPtr
<
T
>&
operator
=
(SharedPtr
<
T
>
&
rv)
{
119
120
if
(
this
->
pn
->
Release()
==
0
)
121
{
122
delete
this
->
pObj;
123
}
124
125
this
->
pObj
=
rv.pObj;
126
rv.pn
->
AddRef();
127
this
->
pn
=
rv.pn;
128
129
return
*
this
;
130
}
131
132
template
<
typename T
>
133
friend
bool
operator
==
(SharedPtr
<
T
>
&
lv, SharedPtr
<
T
>
&
rv);
134
template
<
typename T
>
135
friend
bool
operator
==
(
const
SharedPtr
<
T
>
&
lv,
const
SharedPtr
<
T
>
&
rv);
136
template
<
typename T, typename U
>
137
friend
bool
operator
==
(SharedPtr
<
T
>
&
lv, SharedPtr
<
U
>
&
rv);
138
template
<
typename T, typename U
>
139
friend
bool
operator
==
(
const
SharedPtr
<
T
>
&
lv,
const
SharedPtr
<
U
>
&
rv);
140
141
template
<
typename T
>
142
friend
bool
operator
!=
(SharedPtr
<
T
>
&
lv, SharedPtr
<
T
>
&
rv);
143
template
<
typename T
>
144
friend
bool
operator
!=
(
const
SharedPtr
<
T
>
&
lv,
const
SharedPtr
<
T
>
&
rv);
145
template
<
typename T, typename U
>
146
friend
bool
operator
!=
(SharedPtr
<
T
>
&
lv, SharedPtr
<
U
>
&
rv);
147
template
<
typename T, typename U
>
148
friend
bool
operator
!=
(
const
SharedPtr
<
T
>
&
lv,
const
SharedPtr
<
U
>
&
rv);
149
150
private
:
151
UserObject
<
T
>
*
pObj;
152
UserCount
*
pn;
153
}
;
154
155
template
<
typename T
>
156
bool
operator
==
(SharedPtr
<
T
>
&
lv, SharedPtr
<
T
>
&
rv)
157
{
158
return
lv.
get
()
==
rv.
get
();
159
}
160
161
template
<
typename T
>
162
bool
operator
==
(
const
SharedPtr
<
T
>
&
lv,
const
SharedPtr
<
T
>
&
rv)
163
{
164
return
lv.
get
()
==
rv.
get
();
165
}
166
167
template
<
typename T, typename U
>
168
bool
operator
==
(SharedPtr
<
T
>
&
lv, SharedPtr
<
U
>
&
rv)
169
{
170
return
false
;
171
}
172
173
template
<
typename T, typename U
>
174
bool
operator
==
(
const
SharedPtr
<
T
>
&
lv,
const
SharedPtr
<
U
>
&
rv)
175
{
176
return
false
;
177
}
178
179
template
<
typename T
>
180
bool
operator
!=
(SharedPtr
<
T
>
&
lv, SharedPtr
<
T
>
&
rv)
181
{
182
return
lv.
get
()
!=
rv.
get
();
183
}
184
185
template
<
typename T
>
186
bool
operator
!=
(
const
SharedPtr
<
T
>
&
lv,
const
SharedPtr
<
T
>
&
rv)
187
{
188
return
lv.
get
()
!=
rv.
get
();
189
}
190
191
template
<
typename T, typename U
>
192
bool
operator
!=
(SharedPtr
<
T
>
&
lv, SharedPtr
<
U
>
&
rv)
193
{
194
return
true
;
195
}
196
197
template
<
typename T, typename U
>
198
bool
operator
!=
(
const
SharedPtr
<
T
>
&
lv,
const
SharedPtr
<
U
>
&
rv)
199
{
200
return
true
;
201
}
202
203
#endif
//
__SSA_SHARED_PTR__
希望大家能帮我提一些意见 谢谢
posted on 2008-11-28 17:39
DraculaW
阅读(388)
评论(0)
编辑
收藏
引用
只有注册用户
登录
后才能发表评论。
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
网站导航:
博客园
IT新闻
BlogJava
知识库
博问
管理
Powered by:
C++博客
Copyright © DraculaW