8000 change C++PrimerTest · tthhee/leetcode@19ab23f · GitHub
[go: up one dir, main page]

Skip to content< 8000 /a>

Commit 19ab23f

Browse files
committed
change C++PrimerTest
1 parent 96817c8 commit 19ab23f

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

C++PrimerTest/exercise13.2

69.6 KB
Binary file not shown.

C++PrimerTest/exercise13.2.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <memory>
4+
5+
using namespace std;
6+
//定义行为像值的类
7+
class HasPtr
8+
{
9+
private:
10+
int i;
11+
string* ps;
12+
public:
13+
HasPtr(const string& s = string()):
14+
ps(new string(s)), i(0){}
15+
HasPtr(HasPtr& s):
16+
ps(new string(*s.ps)), i(s.i){}
17+
HasPtr& operator=(const HasPtr& p);
18+
~HasPtr()
19+
{
20+
delete ps;
21+
}
22+
};
23+
24+
HasPtr& HasPtr::operator=(const HasPtr& p)
25+
{
26+
i = p.i;
27+
auto temp = new string(*p.ps);
28+
delete ps;
29+
ps = temp;
30+
return *this;
31+
}
32+
33+
//定义行为像指针的类
34+
class PHasPtr
35+
{
36+
private:
37+
int i;
38+
string* ps;
39+
size_t *use; //记录有多少个对象共享ps成员
40+
public:
41+
PHasPtr(const string& s = string()):
42+
i(0), ps(new string(s)), use(new size_t(1)){}
43+
PHasPtr(const PHasPtr& p):
44+
i(p.i), ps(p.ps), use(p.use) {*use++;}
45+
PHasPtr& operator=(const PHasPtr& s);
46+
~PHasPtr()
47+
{
48+
*use--;
49+
if(*use == 0)
50+
{
51+
delete ps;
52+
delete use;
53+
}
54+
}
55+
};
56+
57+
PHasPtr& PHasPtr::operator=(const PHasPtr& s)
58+
{
59+
i = s.i;
60+
if(--*use == 0)
61+
{
62+
delete ps;
63+
delete use;
64+
}
65+
*(s,use)++;
66+
ps = s.ps;
67+
use = s.use;
68+
return *this;
69+
}
70+
71+
class TreeNode
72+
{
73+
private:
74+
string value;
75+
int count;
76+
TreeNode *left;
77+
size_t *leftnum;
78+
TreeNode *right;
79+
size_t *rightnum;
80+
public:
81+
TreeNode():
82+
value(nullptr), count(0), left(nullptr), right(nullptr),leftnum(new size_t(1)), rightnum(new size_t(1)){}
83+
TreeNode(const TreeNode& t):
84+
value(t.value), count(t.count), left(t.left), leftnum(t.leftnum), right(t.right), rightnum(t.rightnum)
85+
{
86+
*leftnum++;
87+
*rightnum++;
88+
}
89+
~TreeNode()
90+
{
91+
*leftnum--;
92+
*rightnum--;
93+
if(*leftnum == 0)
94+
{
95+
delete left;
96+
delete leftnum;
97+
}
98+
if(*rightnum == 0)
99+
{
100+
delete right;
101+
delete rightnum;
102+
}
103+
}
104+
};
105+
int main()
106+
{
107+
return 0;
108+
}

0 commit comments

Comments
 (0)
0