行为像(值/指针)的类
行为像值的类
- 每个类的数据成员都有一份自己的拷贝.不共享数据成员.
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#include <iostream>
#include <string>
#include <memory>
using namespace std;
class HasPtr{
public:
HasPtr():ps(make_shared<string>()),num(0) {}
HasPtr(const string& s,int sz=0):ps(make_shared<string>(s)),num(sz) {}
HasPtr(const HasPtr& rhs) {
ps = make_shared<string>(*rhs.ps);
num = rhs.get_number();
}
HasPtr& operator= (const HasPtr& rhs) {
ps = make_shared<string>(*rhs.ps);
return *this;
}
int get_number() const { return num; }
int get_number() { return num; }
shared_ptr<string> get_ptr() { return ps; }
shared_ptr<string> get_ptr() const { return ps; }
private:
int num;
shared_ptr<string> ps;
};
int main() {
HasPtr temp1("hello");
HasPtr temp2 = temp1;
cout << "string s= " << *temp1.get_ptr() << endl;
cout << "temp2 s = " << *temp2.get_ptr() << endl;
return 0;
}
行为像指针的类
- 普通构造创建计数器
- 拷贝构造函数,拷贝数据成员,或者指针本身,同时计数器自增
- 拷贝赋值运算,递减左侧运算对象计数器,递增右侧运算对象计数器,并判断计数器==0
- 析构递减计数器,
==0
回收内存HasPtr_ptr1.cpp
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#include <iostream>
#include <string>
using namespace std;
class HasPtr {
public:
HasPtr(const string& s = string(),int sz = 0):ps(new string(s)),i(sz),use(new size_t(1)) {}
HasPtr(const HasPtr& rhs):
ps(rhs.ps),i(rhs.i),use(rhs.use) {
++*use;
}
HasPtr& operator= (const HasPtr& rhs) {
++*rhs.use;
if (--*use == 0) {
cout << "reference count = 0" << endl;
delete ps;
delete use;
}
ps = rhs.ps;
i = rhs.i;
use = rhs.use;
return *this;
}
~HasPtr() {
if (--*use == 0) {
cout << "reference count = 0" << endl;
delete ps;
delete use;
}
}
string get_ps() { return *ps; }
string get_ps() const { return *ps; }
int get_i() { return i; }
int get_i() const { return i; }
private:
string* ps;
int i;
size_t* use;
};
int main() {
HasPtr temp1("hello",11);
HasPtr temp2("world",23);
temp1 = temp2;
cout << "temp1.i " << temp1.get_i() << "\n";
HasPtr temp3 = temp2;
cout << "temp3.i " << temp3.get_i() << "\n";
return 0;
}HasPtr_ptr2.cpp
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#include <iostream>
#include <string>
#include <vector>
#include <initializer_list>
using namespace std;
class HasPtr {
public:
HasPtr():ps(new vector<string>),use(new int(1)) {}
HasPtr(initializer_list<string> i1):ps(new vector<string>(i1)),use(new int(1)) {}
HasPtr(HasPtr& rhs):ps(rhs.ps),use(rhs.use) { ++*use; }
HasPtr& operator= (HasPtr& rhs) {
++*rhs.use;
if (--*use == 0) {
delete ps;
delete use;
}
use = rhs.use;
ps = rhs.ps;
return *this;
}
~HasPtr() {
if (--*use == 0) {
cout << " destructor reference count " << endl;
delete ps;
delete use;
}
}
void push_back(const string& t) { ps->push_back(t); }
void pop_back() {
if (!ps->empty())
ps->pop_back();
else
cout << " vector<string> is empty " << endl;
}
size_t size() const { return ps->size(); }
size_t get_use() const { return *use; }
private:
vector<string>* ps;
int* use;
};
int main() {
HasPtr temp1 = {"a","b","c"};
{
HasPtr temp2 = temp1;
temp2.push_back("d");
cout << "temp2.size()= " << temp2.size() << endl;
cout << "temp1.size()= " << temp1.size() << endl;
cout << "temp1.use = " << temp1.get_use() << " temp2.use = " << temp2.get_use() << endl;
}
cout << "temp1.use = " << temp1.get_use() << endl;
return 0;
}
书上给出的标程:HasPtr_ptr3.cpp
1 |
|
将StrBlob
类通过定义拷贝控制操作使其行为像值
my_StrBlob.h
1 |
|
测试
test.cpp
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!