-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
50 lines (44 loc) · 766 Bytes
/
test.cpp
File metadata and controls
50 lines (44 loc) · 766 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
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include "alloc.h"
using namespace std;
using namespace mystl;
class Test {
static int count;
int No;
public:
Test() {
No = count;
count++;
}
void print() {
cout << this << ":";
cout << "the " << No << "th object" << endl;
}
void* operator new(size_t size);
void operator delete(void* p);
};
void *Test::operator new(size_t n) {
return alloc().allocate(n);
}
void Test::operator delete(void* p) {
alloc().deallocate(p, sizeof(p));
}
int Test::count = 0;
int main() {
Test* p1 = new Test;
p1->print();
Test* p2 = new Test;
p2->print();
delete p1;
p1 = new Test;
p1->print();
Test *p3 = new Test;
p3->print();
delete p1;
delete p2;
delete p3;
return 0;
}