/
test.cpp
70 lines (54 loc) · 1.37 KB
/
test.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
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
class Line {
public:
static int count;
int getLength();
Line(int);
Line(const Line& obj);
~Line();
static int getCount() {
return count;
};
private:
int* ptr;
};
int Line::count = 0;
Line::Line(int length) {
std::cout << "调用构造函数..." << std::endl;
ptr = new int;
*ptr = length;
count++;
}
Line::Line(const Line &obj) {
std::cout << "调用深拷贝构造函数..." << std::endl;
ptr = new int;
*ptr = *obj.ptr;
count++;
}
int Line::getLength() {
std::cout << "Call getLength func..." << std::endl;
return *ptr;
}
Line::~Line() {
std::cout << "释放内存..." << std::endl;
delete ptr;
}
void displayLine(Line line) {
std::cout << "Display Line: " << line.getLength() << std::endl;
}
int main() {
std::cout << "Init..." << std::endl;
Line line1(10);
std::cout << "Going to init line2" << std::endl;
Line line2 = line1;
// std::cout << "Going to display line1" << std::endl;
// displayLine(line1);
// std::cout << "Going to display line2" << std::endl;
// displayLine(line2);
// std::cout << "Finish display lines" << std::endl;
Line* linePtr;
linePtr = &line1;
std::cout << linePtr->getLength() << std::endl;
std::cout << Line::getCount() << std::endl;
return 0;
}