forked from Dashark/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconst-static-new.cpp
More file actions
37 lines (37 loc) · 816 Bytes
/
Copy pathconst-static-new.cpp
File metadata and controls
37 lines (37 loc) · 816 Bytes
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
#include <iostream>
class AnyThing {
private:
static int count; //对象计数器
const int things;
AnyThing() : things(0) { //私有化构造函数
count += 1;
}
public:
~AnyThing() {
count -= 1;
}
static void show() {
std::cout << "How many Object: " << count << std::endl;
}
void show(int i) const {
std::cout << "How many Object: " << count << std::endl;
//things += i;
}
static AnyThing * create() { //工厂方法设计模式
return new AnyThing();
}
static AnyThing * createGroup(int n) { //工厂方法设计模式
return new AnyThing[n];
}
};
int AnyThing::count = 0;
int main() {
const AnyThing *p = AnyThing::create();
AnyThing *pp = AnyThing::createGroup(10);
p->show();
delete p;
AnyThing::show();
delete[] pp;
AnyThing::show();
return 0;
}