-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_6_ExplicitMutable.cpp
More file actions
83 lines (72 loc) · 1.21 KB
/
5_6_ExplicitMutable.cpp
File metadata and controls
83 lines (72 loc) · 1.21 KB
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
#include <stdio.h>
#include <string.h>
class MyString
{
int len = 0;
int mem_capacity = 0;
char* data;
mutable int log = 0;
public:
MyString();
explicit MyString(int size);
MyString(const char* origin);
MyString(const MyString& origin);
~MyString();
int GetLen() const;
int GetCapacity() const;
int GetLog() const;
void Print() const;
};
MyString::MyString(int size) : len(0), mem_capacity(size), data(nullptr), log(0) {}
MyString::MyString(const char* origin) : log(0)
{
mem_capacity = len = strlen(origin);
data = new char[mem_capacity];
for (int i = 0; i < len; ++i)
{
data[i] = origin[i];
}
}
MyString::~MyString()
{
if (data)
{
delete[] data;
}
}
int MyString::GetLen() const
{
++log;
return len;
}
int MyString::GetCapacity() const
{
++log;
return mem_capacity;
}
int MyString::GetLog() const
{
return ++log;
}
void MyString::Print() const
{
++log;
for (int i = 0; i < len; ++i)
{
printf("%c", data[i]);
}
printf("\n");
}
void PrintLen(MyString s)
{
s.Print();
printf("PrintLen: %d\n", s.GetLen());
printf("PrintCapacity: %d\n", s.GetCapacity());
printf("PrintLog: %d\n", s.GetLog());
}
int main(void)
{
PrintLen("aa");
// Error because of Explicit Constructor
// PrintLen(5);
}